Startup.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.IO.Abstractions;
  3. using System.Security.Principal;
  4. using System.Threading.Tasks;
  5. using BO.AppServer.Business.Configuration;
  6. using BO.AppServer.Business.Services;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Builder;
  9. using Microsoft.AspNetCore.Hosting;
  10. using Microsoft.AspNetCore.Mvc.Authorization;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.Extensions.Hosting;
  14. using BO.AppServer.Data.Entity;
  15. using BO.AppServer.Data.Repository;
  16. using BO.AppServer.Web.Data;
  17. using Microsoft.AspNetCore.Http;
  18. using Microsoft.AspNetCore.Identity;
  19. using Quadarax.Foundation.Core.Data.Domain;
  20. using Quadarax.Foundation.Core.Data.Interface.Domain;
  21. namespace AppServer
  22. {
  23. public class Startup
  24. {
  25. private ConfigurationService _srvConfiguration;
  26. private IWebHostEnvironment _env;
  27. public Startup(IConfiguration configuration)
  28. {
  29. Configuration = configuration;
  30. }
  31. public IConfiguration Configuration { get; }
  32. // This method gets called by the runtime. Use this method to add services to the container.
  33. public void ConfigureServices(IServiceCollection services)
  34. {
  35. //services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
  36. // .AddAzureAD(options => Configuration.Bind("AzureAd", options));
  37. // EF enable
  38. services.AddDbContext<BOContext>();
  39. services.AddDbContext<IFDbContext>();
  40. // Identity framework enable
  41. services.AddIdentity<IdentityUser, IdentityRole>()
  42. .AddEntityFrameworkStores<IFDbContext>()
  43. .AddDefaultTokenProviders();
  44. //Use file abstractions
  45. services.AddSingleton<IFileSystem,FileSystem>();
  46. // Inject IPrincipal to get current user in all levels
  47. services.AddHttpContextAccessor();
  48. services.AddTransient<IPrincipal>(provider => provider.GetService<IHttpContextAccessor>().HttpContext?.User);
  49. // Bo Services
  50. var cfg = new Configuration();
  51. Configuration.GetSection("Bo").Bind(cfg);
  52. services.Configure<Configuration>(Configuration.GetSection("Bo"));
  53. services.AddScoped<AccessService>();
  54. services.AddSingleton<ConfigurationService>();
  55. services.AddScoped<UserService>();
  56. services.AddScoped<StatisticsService>();
  57. services.AddScoped<CatalogueService>();
  58. services.AddScoped<WorkspaceService>();
  59. services.AddScoped<StorageService>();
  60. // Bo Repos
  61. services.AddScoped<DataDomain, BOContext>();
  62. services.AddScoped<IDomainContextResolver<DataDomain>, DomainContextResolver<DataDomain>>();
  63. services.AddScoped<IUnitOfWork<DataDomain>, UnitOfWorkDataDomain>();
  64. services.AddScoped<UserRepo>();
  65. services.AddScoped<WorkspaceRepo>();
  66. services.AddScoped<BillingPlanRepo>();
  67. services.AddScoped<MimeTypeRepo>();
  68. services.AddScoped<StatisticRepo>();
  69. services.AddScoped<WorkspaceRepo>();
  70. services.AddScoped<MetadocumentRepo>();
  71. services.AddScoped<ArtifactRepo>();
  72. // Session framework
  73. services.AddDistributedMemoryCache();
  74. services.AddSession(options =>
  75. {
  76. options.IdleTimeout = TimeSpan.Parse(cfg.System.SessionIdleTimeout);
  77. });
  78. services.AddControllersWithViews(options =>
  79. {
  80. var policy = new AuthorizationPolicyBuilder()
  81. .RequireAuthenticatedUser()
  82. .Build();
  83. options.Filters.Add(new AuthorizeFilter(policy));
  84. });
  85. services.AddRazorPages();
  86. }
  87. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  88. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, IServiceProvider service)
  89. {
  90. _env = env;
  91. if (env.IsDevelopment())
  92. {
  93. app.UseDeveloperExceptionPage();
  94. }
  95. else
  96. {
  97. app.UseExceptionHandler("/Home/Error");
  98. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  99. app.UseHsts();
  100. }
  101. //May create IF database structure
  102. using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
  103. {
  104. var context = serviceScope.ServiceProvider.GetRequiredService<IFDbContext>();
  105. context.Database.EnsureCreated();
  106. }
  107. _srvConfiguration = (ConfigurationService)service.GetService(typeof(ConfigurationService));
  108. lifetime.ApplicationStarted.Register(OnApplicationStarted);
  109. app.UseHttpsRedirection();
  110. app.UseStaticFiles();
  111. app.UseRouting();
  112. // Security support
  113. app.UseAuthentication();
  114. app.UseAuthorization();
  115. // Session support
  116. app.UseSession();
  117. app.UseEndpoints(endpoints =>
  118. {
  119. endpoints.MapControllerRoute(
  120. name: "default",
  121. pattern: "{controller=Home}/{action=Index}/{id?}");
  122. endpoints.MapRazorPages();
  123. });
  124. }
  125. private void OnApplicationStarted()
  126. {
  127. _srvConfiguration.Install();
  128. if (_env.IsDevelopment())
  129. {
  130. _srvConfiguration.InstallDev();
  131. }
  132. }
  133. }
  134. }