From 917764273b0f2bfa4d4ca2010e17e56151c9822a Mon Sep 17 00:00:00 2001 From: Reuben Bond Date: Thu, 4 May 2023 14:05:47 -0700 Subject: [PATCH] Remove superfluous UseDeveloperExceptionPage() and AddOptions() calls --- .../Mobile.Bff.Shopping/aggregator/Program.cs | 1 - .../Web.Bff.Shopping/aggregator/Program.cs | 8 +--- src/Services/Ordering/Ordering.API/Program.cs | 42 +++++++------------ .../Ordering.BackgroundTasks/Program.cs | 8 +--- .../Ordering/Ordering.SignalrHub/Program.cs | 8 ++-- src/Services/Payment/Payment.API/Program.cs | 8 ++-- src/Web/WebMVC/Program.cs | 9 +--- src/Web/WebSPA/Program.cs | 5 --- src/Web/WebStatus/Program.cs | 7 +--- src/Web/WebhookClient/Startup.cs | 10 ++--- 10 files changed, 31 insertions(+), 75 deletions(-) diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Program.cs b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Program.cs index 4f31b1da9..945124164 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Program.cs +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Program.cs @@ -15,7 +15,6 @@ builder.Services.AddControllers() builder.Services.AddSwaggerGen(options => { - //options.DescribeAllEnumsAsStrings(); options.SwaggerDoc("v1", new OpenApiInfo { Title = "Shopping Aggregator for Mobile Clients", diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Program.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Program.cs index 691f8975a..8bc62923c 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Program.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Program.cs @@ -1,6 +1,5 @@ var builder = WebApplication.CreateBuilder(args); -builder.Logging.AddConsole(); builder.Services.AddHealthChecks() .AddCheck("self", () => HealthCheckResult.Healthy()) .AddUrlGroup(new Uri(builder.Configuration["CatalogUrlHC"]), name: "catalogapi-check", tags: new string[] { "catalogapi" }) @@ -13,11 +12,7 @@ builder.Services.AddCustomMvc(builder.Configuration) .AddApplicationServices() .AddGrpcServices(); var app = builder.Build(); -if (app.Environment.IsDevelopment()) -{ - app.UseDeveloperExceptionPage(); -} -else +if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } @@ -82,7 +77,6 @@ public static class ServiceCollectionExtensions } public static IServiceCollection AddCustomMvc(this IServiceCollection services, IConfiguration configuration) { - services.AddOptions(); services.Configure(configuration.GetSection("urls")); services.AddControllers() diff --git a/src/Services/Ordering/Ordering.API/Program.cs b/src/Services/Ordering/Ordering.API/Program.cs index a65e7a246..71b202f6a 100644 --- a/src/Services/Ordering/Ordering.API/Program.cs +++ b/src/Services/Ordering/Ordering.API/Program.cs @@ -40,27 +40,20 @@ builder.Services var services = builder.Services; - services.AddMediatR(cfg => - { - cfg.RegisterServicesFromAssemblyContaining(typeof(Program)); - - cfg.AddOpenBehavior(typeof(LoggingBehavior<,>)); - cfg.AddOpenBehavior(typeof(ValidatorBehavior<,>)); - cfg.AddOpenBehavior(typeof(TransactionBehavior<,>)); - }); - - // Register the command validators for the validator behavior (validators based on FluentValidation library) - services.AddSingleton, CancelOrderCommandValidator>(); - services.AddSingleton, CreateOrderCommandValidator>(); - services.AddSingleton>, IdentifiedCommandValidator>(); - services.AddSingleton, ShipOrderCommandValidator>(); +services.AddMediatR(cfg => +{ + cfg.RegisterServicesFromAssemblyContaining(typeof(Program)); + + cfg.AddOpenBehavior(typeof(LoggingBehavior<,>)); + cfg.AddOpenBehavior(typeof(ValidatorBehavior<,>)); + cfg.AddOpenBehavior(typeof(TransactionBehavior<,>)); +}); -/* - // Build the MediatR pipeline - services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>)); - services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(ValidatorBehavior<,>)); - services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(TransactionBehavior<,>)); -*/ +// Register the command validators for the validator behavior (validators based on FluentValidation library) +services.AddSingleton, CancelOrderCommandValidator>(); +services.AddSingleton, CreateOrderCommandValidator>(); +services.AddSingleton>, IdentifiedCommandValidator>(); +services.AddSingleton, ShipOrderCommandValidator>(); var queriesConnectionString = builder.Configuration["ConnectionString"]; @@ -78,19 +71,17 @@ services.AddSingleton, UserCheckoutAcceptedIntegrationEventHandler>(); var app = builder.Build(); -if (app.Environment.IsDevelopment()) -{ - app.UseDeveloperExceptionPage(); -} -else +if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } + var pathBase = app.Configuration["PATH_BASE"]; if (!string.IsNullOrEmpty(pathBase)) { app.UsePathBase(pathBase); } + app.UseSwagger().UseSwaggerUI(c => { c.SwaggerEndpoint($"{(!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty)}/swagger/v1/swagger.json", "Ordering.API V1"); @@ -336,7 +327,6 @@ static class CustomExtensionsMethods public static IServiceCollection AddCustomConfiguration(this IServiceCollection services, IConfiguration configuration) { - services.AddOptions(); services.Configure(configuration); services.Configure(options => { diff --git a/src/Services/Ordering/Ordering.BackgroundTasks/Program.cs b/src/Services/Ordering/Ordering.BackgroundTasks/Program.cs index de689fd33..0be966e96 100644 --- a/src/Services/Ordering/Ordering.BackgroundTasks/Program.cs +++ b/src/Services/Ordering/Ordering.BackgroundTasks/Program.cs @@ -9,18 +9,14 @@ builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.Environment builder.Configuration.AddEnvironmentVariables(); builder.Services.AddCustomHealthCheck(builder.Configuration) .Configure(builder.Configuration) - .AddOptions() .AddHostedService() .AddEventBus(builder.Configuration); var app = builder.Build(); -if (app.Environment.IsDevelopment()) -{ - app.UseDeveloperExceptionPage(); -} -else +if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } + app.UseRouting(); app.MapHealthChecks("/hc", new HealthCheckOptions() diff --git a/src/Services/Ordering/Ordering.SignalrHub/Program.cs b/src/Services/Ordering/Ordering.SignalrHub/Program.cs index 718f7760d..96fc26e0b 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/Program.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/Program.cs @@ -85,19 +85,17 @@ builder.Services.AddSingleton, OrderStatusChangedToSubmittedIntegrationEventHandler>(); var app = builder.Build(); -if (app.Environment.IsDevelopment()) -{ - app.UseDeveloperExceptionPage(); -} -else +if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } + var pathBase = builder.Configuration["PATH_BASE"]; if (!string.IsNullOrEmpty(pathBase)) { app.UsePathBase(pathBase); } + app.UseRouting(); app.UseCors("CorsPolicy"); app.UseAuthentication(); diff --git a/src/Services/Payment/Payment.API/Program.cs b/src/Services/Payment/Payment.API/Program.cs index 8c95c4b0d..c1d2fe5f1 100644 --- a/src/Services/Payment/Payment.API/Program.cs +++ b/src/Services/Payment/Payment.API/Program.cs @@ -57,19 +57,17 @@ else } RegisterEventBus(builder.Services); var app = builder.Build(); -if (app.Environment.IsDevelopment()) -{ - app.UseDeveloperExceptionPage(); -} -else +if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } + var pathBase = app.Configuration["PATH_BASE"]; if (!string.IsNullOrEmpty(pathBase)) { app.UsePathBase(pathBase); } + ConfigureEventBus(app); app.UseRouting(); diff --git a/src/Web/WebMVC/Program.cs b/src/Web/WebMVC/Program.cs index 55e7a1027..fff566c19 100644 --- a/src/Web/WebMVC/Program.cs +++ b/src/Web/WebMVC/Program.cs @@ -12,11 +12,7 @@ builder.WebHost.CaptureStartupErrors(false); var app = builder.Build(); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub"); -if (app.Environment.IsDevelopment()) -{ - app.UseDeveloperExceptionPage(); -} -else +if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); } @@ -72,8 +68,7 @@ static void AddHealthChecks(WebApplicationBuilder builder) static void AddCustomMvc(WebApplicationBuilder builder) { - builder.Services.AddOptions() - .Configure(builder.Configuration) + builder.Services.Configure(builder.Configuration) .AddSession() .AddDistributedMemoryCache(); diff --git a/src/Web/WebSPA/Program.cs b/src/Web/WebSPA/Program.cs index 52f090b67..145151df7 100644 --- a/src/Web/WebSPA/Program.cs +++ b/src/Web/WebSPA/Program.cs @@ -40,11 +40,6 @@ builder.Logging.AddAzureWebAppDiagnostics(); var app = builder.Build(); -if (app.Environment.IsDevelopment()) -{ - app.UseDeveloperExceptionPage(); -} - // Here we add Angular default Anti-forgery cookie name on first load. https://angular.io/guide/http#security-xsrf-protection // This cookie will be read by Angular app and its value will be sent back to the application as the header configured in .AddAntiforgery() var antiForgery = app.Services.GetRequiredService(); diff --git a/src/Web/WebStatus/Program.cs b/src/Web/WebStatus/Program.cs index 96c91271e..d9eadc5d5 100644 --- a/src/Web/WebStatus/Program.cs +++ b/src/Web/WebStatus/Program.cs @@ -12,7 +12,6 @@ builder.WebHost.CaptureStartupErrors(false); builder.Services.AddApplicationInsightsTelemetry(builder.Configuration); builder.Services.AddApplicationInsightsKubernetesEnricher(); builder.Services.AddMvc(); -builder.Services.AddOptions(); builder.Services.AddHealthChecks() .AddCheck("self", () => HealthCheckResult.Healthy()); builder.Services @@ -21,11 +20,7 @@ builder.Services var app = builder.Build(); -if (app.Environment.IsDevelopment()) -{ - app.UseDeveloperExceptionPage(); -} -else +if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } diff --git a/src/Web/WebhookClient/Startup.cs b/src/Web/WebhookClient/Startup.cs index af68fa66f..61d91030f 100644 --- a/src/Web/WebhookClient/Startup.cs +++ b/src/Web/WebhookClient/Startup.cs @@ -1,4 +1,4 @@ -namespace WebhookClient; +namespace WebhookClient; public class Startup { @@ -36,15 +36,12 @@ public class Startup app.UsePathBase(pathBase); } - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else + if (!env.IsDevelopment()) { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. } + app.Map("/check", capp => { capp.Run(async (context) => @@ -97,7 +94,6 @@ static class ServiceExtensions { public static IServiceCollection AddConfiguration(this IServiceCollection services, IConfiguration configuration) { - services.AddOptions(); services.Configure(configuration); return services; }