Startup.cs 3.6 KB

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