private static AutofacServiceProvider Configure() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); ILoggerFactory loggerFactory = new LoggerFactory() .AddConsole(Configuration.GetSection("Logging")) .AddDebug() .AddConsole(); var serviceCollection = new ServiceCollection(); serviceCollection.AddOptions(); serviceCollection.Configure <ConnectionStrings>(Configuration.GetSection("ConnectionStrings")); serviceCollection.Configure <LoadSettings>(Configuration.GetSection("LoadSettings")); serviceCollection.AddSingleton(loggerFactory); // Add first my already configured instance serviceCollection.AddLogging(); var autoBuilder = new ContainerBuilder(); autoBuilder.Populate(serviceCollection); var diHelper = new DiHelper(); var diAssemblies = Configuration.GetValue <string>("DiAssemblies:List"); var assemblies = diHelper.GetAssemblies(diAssemblies.Split(',').ToList()); // Console.Write(diAssemblies); autoBuilder.RegisterAssemblyModules(assemblies); Container = autoBuilder.Build(); // Create the IServiceProvider based on the container. return(new AutofacServiceProvider(Container)); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.Configure <ConnectionStrings>(Configuration.GetSection("ConnectionStrings")); services.Configure <DiAssemblies>(Configuration.GetSection("DiAssemblies")); services.AddCors(options => { options.AddPolicy("CorsPolicy", x => x.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); services.AddCors(); services.AddIdentity <Identity.ApplicationUser, IdentityRole>() .AddEntityFrameworkStores <Identity.Context>() .AddDefaultTokenProviders(); services.AddAuthentication(sharedOptions => { sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; sharedOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; sharedOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.SaveToken = true; options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters() { IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(), ValidIssuer = AuthOptions.Issuer, ValidAudience = AuthOptions.Audience, }; options.Events = new JwtBearerEvents { OnAuthenticationFailed = context => { return(Task.CompletedTask); }, OnMessageReceived = context => { return(Task.CompletedTask); }, OnChallenge = context => { return(Task.CompletedTask); } }; }); services.AddApplicationInsightsTelemetry(this.Configuration); services.AddMvc().AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; }); services.AddMvc(config => { config.Filters.Add(typeof(ExceptionFilter)); }); var builder = new ContainerBuilder(); // Register dependencies, populate the services from // the collection, and build the container. If you want // to dispose of the container at the end of the app, // be sure to keep a reference to it as a property or field. //builder.RegisterType<MyType>().As<IMyType>(); builder.Populate(services); var diHelper = new DiHelper(); var diAssemblies = Configuration.GetValue <string>("DiAssemblies:List"); var assemblies = diHelper.GetAssemblies(diAssemblies.Split(',').ToList()); builder.RegisterAssemblyModules(assemblies); ApplicationContainer = builder.Build(); // Create the IServiceProvider based on the container. return(new AutofacServiceProvider(ApplicationContainer)); }