public static void Config(HttpConfiguration config, Assembly webapiAssembly, IEnumerable <Assembly> assemblies) { var builder = new ContainerBuilder(); builder.RegisterApiControllers(webapiAssembly); builder.RegisterAssemblyTypes(webapiAssembly).AsImplementedInterfaces(); builder.RegisterAssemblyTypes(assemblies.ToArray()).AsImplementedInterfaces(); AutofacRegister.Register(builder); var container = builder.Build(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); ObjectContainers.Current = container; }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddDbContext <DefaultContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default"), b => b.MigrationsAssembly("ApplicationCore")) ); services.AddIdentity <User, IdentityRole>(options => { options.User.RequireUniqueEmail = true; }) .AddEntityFrameworkStores <DefaultContext>() .AddDefaultTokenProviders(); services.Configure <AppSettings>(Configuration.GetSection("AppSettings")); services.Configure <MongoDBSettings>(Configuration.GetSection("MongoDBSettings")); services.Configure <RootSubjectSettings>(Configuration.GetSection("RootSubjectSettings")); services.Configure <AuthSettings>(Configuration.GetSection("AuthSettings")); services.Configure <SubscribesSettings>(Configuration.GetSection("SubscribesSettings")); services.Configure <EcPaySettings>(Configuration.GetSection("EcPaySettings")); services.Configure <CloudStorageSettings>(Configuration.GetSection("CloudStorageSettings")); services.Configure <AdminSettings>(Configuration.GetSection("AdminSettings")); services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("Default"))); services.AddHangfireServer(); var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["AuthSettings:SecurityKey"])); string issuer = Configuration["AppSettings:Name"]; string audience = ClientUrl; string securityKey = Configuration["AuthSettings:SecurityKey"]; int tokenValidHours = Convert.ToInt32(Configuration["AuthSettings:TokenValidHours"]); services.AddJwtBearer(tokenValidHours, issuer, audience, securityKey); services.AddSwagger("PosterExamStarter", "v1"); services.AddDtoMapper(); services.AddAuthorization(options => { options.AddPolicy("Subscriber", policy => policy.Requirements.Add(new HasPermissionRequirement(Permissions.Subscriber))); options.AddPolicy("Admin", policy => policy.Requirements.Add(new HasPermissionRequirement(Permissions.Admin))); }); services.AddCors(options => { options.AddPolicy("Api", builder => { builder.WithOrigins(ClientUrl) .AllowAnyHeader() .AllowAnyMethod().AllowCredentials(); }); options.AddPolicy("Admin", builder => { builder.WithOrigins(AdminUrl) .AllowAnyHeader() .AllowAnyMethod(); }); options.AddPolicy("Global", builder => { builder.WithOrigins(ClientUrl, AdminUrl) .AllowAnyHeader() .AllowAnyMethod(); }); options.AddPolicy("EcPay", builder => { builder.WithOrigins(Configuration["EcPaySettings:Url"]) .AllowAnyHeader() .AllowAnyMethod(); }); }); services.AddControllers(); services.AddSignalR(); services.AddHttpClient(Consts.Google, c => { c.BaseAddress = new Uri("https://www.google.com"); }); services.AddSingleton <IHubConnectionManager, HubConnectionManager>(); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped <Web.Services.IThirdPartyPayService, Web.Services.EcPayService>(); services.AddScoped <Web.Services.ISubscribesService, Web.Services.SubscribesService>(); return(AutofacRegister.Register(services)); }