Startup.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Hosting;
  7. using System;
  8. using System.Collections.Generic;
  9. using VCommon.Ioc;
  10. using VCommon.Logging;
  11. using VCommon.VApplication;
  12. using VCommon.VOpenApi.VAspNetCore;
  13. using VCommon.VSwaggerUI;
  14. using VCommon.VSwaggerUI.VAspNetCore;
  15. using VCommonCoreExample.AppServices.Platform;
  16. using VCommonCoreExample.AppServices.Session;
  17. using VCommonCoreExample.AppServices.System.Roles;
  18. using VCommonCoreExample.AppServices.System.Users;
  19. using VCommonCoreExample.AppServices.UserProfiles;
  20. using VCommonCoreExample.Configuration;
  21. using VCommonCoreExample.EntityFrameworkCore;
  22. namespace VCommonCoreExample
  23. {
  24. public class Startup
  25. {
  26. private readonly IConfigurationRoot _appConfiguration;
  27. private readonly string _contentRootPath;
  28. public Startup(IWebHostEnvironment env)
  29. {
  30. _contentRootPath = env.ContentRootPath;
  31. _appConfiguration = env.GetAppConfiguration();
  32. }
  33. // This method gets called by the runtime. Use this method to add services to the container.
  34. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
  35. public void ConfigureServices(IServiceCollection services)
  36. {
  37. }
  38. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  39. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  40. {
  41. var dbg = false;
  42. if (env.IsDevelopment())
  43. {
  44. app.UseDeveloperExceptionPage();
  45. dbg = true;
  46. }
  47. Logger.Init(new VJsonFileLogger(enableAll: dbg));
  48. Logger.EnableDebugLevel = dbg;
  49. Logger.Info("Application Init");
  50. AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
  51. {
  52. Logger.Fatal("AppDomain.CurrentDomain.UnhandledException", e);
  53. };
  54. var connectionString = _appConfiguration.GetConnectionString(ProjectConst.ConnectionStringName);
  55. var dbContextOptionsBuilder = new DbContextOptionsBuilder<ExampleDbContext>();
  56. ExampleDbContextConfig.Configure(dbContextOptionsBuilder, connectionString);
  57. var rootContainer = new IocManager();
  58. rootContainer.RegisterInstanceToContainer<IConfiguration>(_appConfiguration);
  59. rootContainer.RegisterInstanceToContainer(dbContextOptionsBuilder.Options);
  60. rootContainer.RegisterManually<VEmptySession>();
  61. if (dbg) app.UseSwaggerUiMiddleware(SwaggerUiResourceProvider.Version1);
  62. ApiMiddleware.Init(new Dictionary<string, Type>
  63. {
  64. {"Session",typeof(ISessionService) },
  65. {"SystemUser",typeof(IUserService) },
  66. {"SystemRole",typeof(IRoleService) },
  67. {"PlatformTenant",typeof(ITenantService) },
  68. {"UserProfile",typeof(IUserProfileService) },
  69. }, iocManager: rootContainer, docGen: dbg, isDebuggingEnabled: dbg);
  70. app.UseMiddleware<ApiMiddleware>();
  71. app.Use(async (context, next) =>
  72. {
  73. if (dbg && context.Request.Path.Value == "/")
  74. {
  75. context.Response.Redirect("/swagger/");
  76. }
  77. else await next();
  78. });
  79. Logger.Info("Application Init Done");
  80. }
  81. }
  82. }