using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using VCommon.Ioc; using VCommon.Logging; using VCommon.VApplication; using VCommon.VOpenApi.VAspNetCore; using VCommon.VSwaggerUI; using VCommon.VSwaggerUI.VAspNetCore; using VCommonCoreExample.AppServices.Platform; using VCommonCoreExample.AppServices.Session; using VCommonCoreExample.AppServices.System.Roles; using VCommonCoreExample.AppServices.System.Users; using VCommonCoreExample.AppServices.UserProfiles; using VCommonCoreExample.Configuration; using VCommonCoreExample.EntityFrameworkCore; namespace VCommonCoreExample { public class Startup { private readonly IConfigurationRoot _appConfiguration; private readonly string _contentRootPath; public Startup(IWebHostEnvironment env) { _contentRootPath = env.ContentRootPath; _appConfiguration = env.GetAppConfiguration(); } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { var dbg = false; if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); dbg = true; } Logger.Init(new VJsonFileLogger(enableAll: dbg)); Logger.EnableDebugLevel = dbg; Logger.Info("Application Init"); AppDomain.CurrentDomain.UnhandledException += (sender, e) => { Logger.Fatal("AppDomain.CurrentDomain.UnhandledException", e); }; var connectionString = _appConfiguration.GetConnectionString(ProjectConst.ConnectionStringName); var dbContextOptionsBuilder = new DbContextOptionsBuilder(); ExampleDbContextConfig.Configure(dbContextOptionsBuilder, connectionString); var rootContainer = new IocManager(); rootContainer.RegisterInstanceToContainer(_appConfiguration); rootContainer.RegisterInstanceToContainer(dbContextOptionsBuilder.Options); rootContainer.RegisterManually(); if (dbg) app.UseSwaggerUiMiddleware(SwaggerUiResourceProvider.Version1); ApiMiddleware.Init(new Dictionary { {"Session",typeof(ISessionService) }, {"SystemUser",typeof(IUserService) }, {"SystemRole",typeof(IRoleService) }, {"PlatformTenant",typeof(ITenantService) }, {"UserProfile",typeof(IUserProfileService) }, }, iocManager: rootContainer, docGen: dbg, isDebuggingEnabled: dbg); app.UseMiddleware(); app.Use(async (context, next) => { if (dbg && context.Request.Path.Value == "/") { context.Response.Redirect("/swagger/"); } else await next(); }); Logger.Info("Application Init Done"); } } }