public virtual IServiceProvider ConfigureServices(IServiceCollection services) { // CorrelationId should be set in a very begining CorrelationContext.SetCorrelationId(); mainThreadCorrelationId = CorrelationContext.CorrelationId; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ApplicationSettings = new ApplicationSettings(); Configuration.GetSection(typeof(ApplicationSettings).Name).Bind(ApplicationSettings); // add custom configuration section services.Configure <ApplicationSettings>(opt => Configuration.GetSection(typeof(ApplicationSettings).Name)); services.AddSingleton(ApplicationSettings); // add AutoMapper services.ConfigureAutoMapper(ConfigureAutoMapper); // add healthState reporter services.AddTransient <IHealthReporter>(provider => new HealthReporter(Application.ReportHealthStateAction, Application.ReportRecurrentHealthStateAction)); // add version resolver services.AddTransient <IVersionInfoResolver>(provider => new VersionInfoResolver(Application.GetCodePackageVersionFunction, Application.GetDataPackageVersionFunction)); // add services to container ConfigureContainer(services); services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; // false by default if (ApplicationSettings.Hosting.AuthenticationRequired) { JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services .AddAuthorization(opt => { opt.AddPolicy("policy", builder => { builder.RequireScope(ApplicationSettings.Hosting.ScopeName); }); }) .AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(authOptions => { authOptions.JwtBackChannelHandler = new CorrelatedHttpClientHandler(); authOptions.Authority = ApplicationSettings.Hosting.AuthorityUrl; authOptions.SupportedTokens = SupportedTokens.Jwt; }); Claim claim = null; if (!string.IsNullOrEmpty(ApplicationSettings.Hosting.ClientId) && StartupParameters.SecurityConfiguration != null) { claim = StartupParameters.SecurityConfiguration(ApplicationSettings.Hosting.ClientId); } options.Filters.Add(new AuthorizeCustomFilter(claim)); } if (ApplicationSettings.Hosting.HttpsRequired) { options.Filters.Add(new RequireHttpsFilter()); options.Filters.Add(new HSTSAttribute()); } options.Filters.Add(new ApiExceptionHandlingFilter( ApplicationSettings.Hosting.ShowErrorDetails, ApplicationSettings.Hosting.IsGateway)); }).AddControllersAsServices(); services.AddOData(); // Workaround: https://github.com/OData/WebApi/issues/1177 services.AddMvcCore(options => { foreach (var outputFormatter in options.OutputFormatters.OfType <ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0)) { outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); } foreach (var inputFormatter in options.InputFormatters.OfType <ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0)) { inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); } }); services.AddTransient <IConfigureOptions <MvcJsonOptions>, JsonOptionsSetup>(); services.AddSingleton(JsonOptionsSetup.Settings); services.AddMemoryCache(); if (ApplicationSettings.Hosting.SwaggerEnabled) { services.ConfigureSwagger(GetType(), ApplicationSettings.Hosting.AuthenticationRequired); // todo: add swagger redirect } RegisterServiceBuses(services); services.RegisterModule <WebCommunicationModule>(); Container = ConfigureServiceProvider(services); return(Container); }