using System; using System.Collections.Generic; using System.Text; using Autofac; using Autofac.Extensions.DependencyInjection; using iPackage.Core.Web.Extensions; using iPackage.Core.Web.Models.Settings; using iPackage.Core.Web.Repositories; using iPackage.Core.Web.Services.Contracts; using iPackage.Core.Web.WebFramework.Configurations; using iPackage.Core.Web.WebFramework.Middlewares; using iPackage.Extensions; using iPackage.Models.Service; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Newtonsoft.Json; namespace iPackage.Core.Web { public abstract class iPackageStartup { private SiteSettings _siteSetting; public static string BaseUrl; public iPackageStartup(Microsoft.AspNetCore.Hosting.IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); this.Configuration = builder.Build(); _siteSetting = Configuration.GetSection(nameof(SiteSettings)).Get(); AutoMapperConfig.ConfigurationMapper(); BaseUrl = _siteSetting.BaseUrl; } public IConfigurationRoot Configuration { get; private set; } public ILifetimeScope AutofacContainer { get; private set; } // This method gets called by the runtime. Use this method to add services to the container. public virtual void ConfigureServices(IServiceCollection services) { //services.AddAutoFacServerContainer(); services.Configure(Configuration.GetSection(nameof(SiteSettings))); services.AddCustomSwagger(BaseUrl); services.AddCustomApiVersioning(); services.AddJwtCustomAuthentication(_siteSetting.JwtSettings); services.AddSignalR(); services.AddMemoryCache(); services.AddControllers(options => { options.Filters.Add(new AuthorizeFilter()); }) .AddControllersAsServices() .AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore ); JsonConvert.DefaultSettings = () => new JsonSerializerSettings { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore }; services.AddCors(options => options.AddPolicy("CorsPolicy", builder => { builder.AllowAnyMethod() .AllowAnyHeader() .SetIsOriginAllowed(_ => true) .AllowCredentials(); })); } public virtual void ConfigureContainer(ContainerBuilder builder) { var assembly = typeof(YourStartUpType).Assembly; builder.RegisterAssemblyTypes(assembly) .AssignableTo() .AsImplementedInterfaces() .InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(typeof(IRepositoryWrapper).Assembly) .AssignableTo() .AsImplementedInterfaces() .InstancePerLifetimeScope(); builder.RegisterAssemblyTypes(typeof(IDbService).Assembly) .AssignableTo() .AsImplementedInterfaces() .InstancePerLifetimeScope(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public virtual async void Configure(IApplicationBuilder app, IWebHostEnvironment env) { this.AutofacContainer = app.ApplicationServices.GetAutofacRoot(); app.UseExceptionHandlerMiddleware(); if (env.IsDevelopment()) { //app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthentication(); app.UseCustomSwaager(BaseUrl); app.UseDbInitializer(); app.UseAuthorization(); app.UseCors("CorsPolicy"); } } }