Startup.cs 5.7 KB

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