// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvcCore(o => { o.Filters.Add(typeof(GlobalExceptionFilter)); }) .AddApiExplorer() .AddAuthorization() .AddJsonFormatters(); var authenticationsConfiguration = new AuthenticationsConfiguration(); Configuration.GetSection("Authentications").Bind(authenticationsConfiguration); services.AddAuthentication("Bearer") .AddIdentityServerAuthentication(options => { options.ApiSecret = authenticationsConfiguration.Bearer.ApiSecret; options.Authority = authenticationsConfiguration.Bearer.Authority; options.RequireHttpsMetadata = authenticationsConfiguration.Bearer.RequireHttpsMetadata; options.ApiName = authenticationsConfiguration.Bearer.ApiName; }); services.AddApiVersioning(options => { options.AssumeDefaultVersionWhenUnspecified = true; }); services.AddCors(); services.AddSignalR(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "API doc", Version = "v1" }); }); var containerBuilder = new ContainerBuilder(); containerBuilder.Populate(services); DIModulesManager.RegisterModules(containerBuilder); ApplicationContainer = containerBuilder.Build(); return(new AutofacServiceProvider(ApplicationContainer)); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddMvcCore(options => { options.Filters.Add <ValidateModelStateFilter>(); }) .AddFluentValidation(configuration => configuration.RegisterValidatorsFromAssemblyContaining <Startup>()) .AddApiExplorer() .AddAuthorization() .AddJsonFormatters(); var authenticationsConfiguration = new AuthenticationsConfiguration(); _configuration.GetSection("Authentications").Bind(authenticationsConfiguration); services.AddAuthentication("Bearer") .AddIdentityServerAuthentication(options => { options.ApiSecret = authenticationsConfiguration.Bearer.ApiSecret; options.Authority = authenticationsConfiguration.Bearer.Authority; options.RequireHttpsMetadata = authenticationsConfiguration.Bearer.RequireHttpsMetadata; options.ApiName = authenticationsConfiguration.Bearer.ApiName; }); services.AddSingleton <IAuthorizationHandler, UserLoginAuthorizationHandler>(); services.AddApiVersioning(options => { options.AssumeDefaultVersionWhenUnspecified = true; options.DefaultApiVersion = new ApiVersion(1, 0); options.ReportApiVersions = true; options.ApiVersionReader = new HeaderApiVersionReader("api-version"); }); services.AddCors(); services.AddSignalR(); services.AddSwaggerGen(c => { c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey" }); c.SwaggerDoc("v1.0", new Info { Title = "API doc v1.0", Version = "v1.0" }); c.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> > { { "Bearer", new string[0] } }); c.DocInclusionPredicate((docName, apiDesc) => { var actionApiVersionModel = apiDesc.ActionDescriptor?.GetApiVersion(); // would mean this action is unversioned and should be included everywhere if (actionApiVersionModel == null) { return(true); } if (actionApiVersionModel.DeclaredApiVersions.Any()) { return(actionApiVersionModel.DeclaredApiVersions.Any(v => $"v{v.ToString()}" == docName)); } return(actionApiVersionModel.ImplementedApiVersions.Any(v => $"v{v.ToString()}" == docName)); }); c.OperationFilter <ApiVersionOperationFilter>(); }); var builder = new ContainerBuilder(); builder.RegisterSolutionModules(); builder.AddLogger(); builder.Populate(services); _applicationContainer = builder.Build(); return(new AutofacServiceProvider(_applicationContainer)); }