namespace Microsoft.eShopOnContainers.Services.Marketing.API { using AspNetCore.Builder; using AspNetCore.Hosting; using AspNetCore.Http; using Autofac; using Autofac.Extensions.DependencyInjection; using Azure.ServiceBus; using BuildingBlocks.EventBus; using BuildingBlocks.EventBus.Abstractions; using BuildingBlocks.EventBusRabbitMQ; using BuildingBlocks.EventBusServiceBus; using EntityFrameworkCore; using Extensions.Configuration; using Extensions.DependencyInjection; using Extensions.HealthChecks; using Extensions.Logging; using Infrastructure; using Infrastructure.Filters; using Infrastructure.Repositories; using Infrastructure.Services; using IntegrationEvents.Events; using Marketing.API.IntegrationEvents.Handlers; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore.Diagnostics; using RabbitMQ.Client; using Swashbuckle.AspNetCore.Swagger; using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Reflection; using System.Threading.Tasks; public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddApplicationInsightsTelemetry(Configuration); if (Configuration.GetValue("OrchestratorType").Equals("K8S")) { // Enable K8s telemetry initializer services.EnableKubernetes(); } // Add framework services. services.AddMvc(options => { options.Filters.Add(typeof(HttpGlobalExceptionFilter)); }).AddControllersAsServices(); //Injecting Controllers themselves thru DIFor further info see: http://docs.autofac.org/en/latest/integration/aspnetcore.html#controllers-as-services services.Configure(Configuration); ConfigureAuthService(services); services.AddHealthChecks(checks => { checks.AddValueTaskCheck("HTTP Endpoint", () => new ValueTask(HealthCheckResult.Healthy("Ok"))); var accountName = Configuration.GetValue("AzureStorageAccountName"); var accountKey = Configuration.GetValue("AzureStorageAccountKey"); if (!string.IsNullOrEmpty(accountName) && !string.IsNullOrEmpty(accountKey)) { checks.AddAzureBlobStorageCheck(accountName, accountKey); } }); services.AddDbContext(options => { options.UseSqlServer(Configuration["ConnectionString"], sqlServerOptionsAction: sqlOptions => { sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name); //Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); // Changing default behavior when client evaluation occurs to throw. // Default in EF Core would be to log a warning when client evaluation is performed. options.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning)); //Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval }); if (Configuration.GetValue("AzureServiceBusEnabled")) { services.AddSingleton(sp => { var logger = sp.GetRequiredService>(); var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString); return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger); }); } else { services.AddSingleton(sp => { var logger = sp.GetRequiredService>(); var factory = new ConnectionFactory() { HostName = Configuration["EventBusConnection"] }; if (!string.IsNullOrEmpty(Configuration["EventBusUserName"])) { factory.UserName = Configuration["EventBusUserName"]; } if (!string.IsNullOrEmpty(Configuration["EventBusPassword"])) { factory.Password = Configuration["EventBusPassword"]; } return new DefaultRabbitMQPersistentConnection(factory, logger); }); } // Add framework services. services.AddSwaggerGen(options => { options.DescribeAllEnumsAsStrings(); options.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "Marketing HTTP API", Version = "v1", Description = "The Marketing Service HTTP API", TermsOfService = "Terms Of Service" }); options.AddSecurityDefinition("oauth2", new OAuth2Scheme { Type = "oauth2", Flow = "implicit", AuthorizationUrl = $"{Configuration.GetValue("IdentityUrlExternal")}/connect/authorize", TokenUrl = $"{Configuration.GetValue("IdentityUrlExternal")}/connect/token", Scopes = new Dictionary() { { "marketing", "Marketing API" } } }); options.OperationFilter(); }); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials()); }); RegisterEventBus(services); services.AddTransient(); services.AddSingleton(); services.AddTransient(); services.AddOptions(); //configure autofac var container = new ContainerBuilder(); container.Populate(services); return new AutofacServiceProvider(container.Build()); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory) { loggerFactory.AddAzureWebAppDiagnostics(); loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace); var pathBase = Configuration["PATH_BASE"]; if (!string.IsNullOrEmpty(pathBase)) { app.UsePathBase(pathBase); } app.UseCors("CorsPolicy"); ConfigureAuth(app); app.UseMvcWithDefaultRoute(); app.UseSwagger() .UseSwaggerUI(c => { c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "My API V1"); c.ConfigureOAuth2("marketingswaggerui", "", "", "Marketing Swagger UI"); }); ConfigureEventBus(app); } private void ConfigureAuthService(IServiceCollection services) { // prevent from mapping "sub" claim to nameidentifier. JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthentication(options=> { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.Authority = Configuration.GetValue("IdentityUrl"); options.Audience = "marketing"; options.RequireHttpsMetadata = false; }); } private void RegisterEventBus(IServiceCollection services) { if (Configuration.GetValue("AzureServiceBusEnabled")) { services.AddSingleton(sp => { var serviceBusPersisterConnection = sp.GetRequiredService(); var iLifetimeScope = sp.GetRequiredService(); var logger = sp.GetRequiredService>(); var eventBusSubcriptionsManager = sp.GetRequiredService(); var subscriptionClientName = Configuration["SubscriptionClientName"]; return new EventBusServiceBus(serviceBusPersisterConnection, logger, eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope); }); } else { services.AddSingleton(); } services.AddSingleton(); services.AddTransient(); } private void ConfigureEventBus(IApplicationBuilder app) { var eventBus = app.ApplicationServices.GetRequiredService(); eventBus.Subscribe(); } protected virtual void ConfigureAuth(IApplicationBuilder app) { app.UseAuthentication(); } } }