Remove superfluous UseDeveloperExceptionPage() and AddOptions() calls

This commit is contained in:
Reuben Bond 2023-05-04 14:05:47 -07:00
parent e2d8590a26
commit 917764273b
10 changed files with 31 additions and 75 deletions

View File

@ -15,7 +15,6 @@ builder.Services.AddControllers()
builder.Services.AddSwaggerGen(options => builder.Services.AddSwaggerGen(options =>
{ {
//options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new OpenApiInfo options.SwaggerDoc("v1", new OpenApiInfo
{ {
Title = "Shopping Aggregator for Mobile Clients", Title = "Shopping Aggregator for Mobile Clients",

View File

@ -1,6 +1,5 @@
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddConsole();
builder.Services.AddHealthChecks() builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy()) .AddCheck("self", () => HealthCheckResult.Healthy())
.AddUrlGroup(new Uri(builder.Configuration["CatalogUrlHC"]), name: "catalogapi-check", tags: new string[] { "catalogapi" }) .AddUrlGroup(new Uri(builder.Configuration["CatalogUrlHC"]), name: "catalogapi-check", tags: new string[] { "catalogapi" })
@ -13,11 +12,7 @@ builder.Services.AddCustomMvc(builder.Configuration)
.AddApplicationServices() .AddApplicationServices()
.AddGrpcServices(); .AddGrpcServices();
var app = builder.Build(); var app = builder.Build();
if (app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{ {
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
} }
@ -82,7 +77,6 @@ public static class ServiceCollectionExtensions
} }
public static IServiceCollection AddCustomMvc(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddCustomMvc(this IServiceCollection services, IConfiguration configuration)
{ {
services.AddOptions();
services.Configure<UrlsConfig>(configuration.GetSection("urls")); services.Configure<UrlsConfig>(configuration.GetSection("urls"));
services.AddControllers() services.AddControllers()

View File

@ -40,27 +40,20 @@ builder.Services
var services = builder.Services; var services = builder.Services;
services.AddMediatR(cfg => services.AddMediatR(cfg =>
{ {
cfg.RegisterServicesFromAssemblyContaining(typeof(Program)); cfg.RegisterServicesFromAssemblyContaining(typeof(Program));
cfg.AddOpenBehavior(typeof(LoggingBehavior<,>)); cfg.AddOpenBehavior(typeof(LoggingBehavior<,>));
cfg.AddOpenBehavior(typeof(ValidatorBehavior<,>)); cfg.AddOpenBehavior(typeof(ValidatorBehavior<,>));
cfg.AddOpenBehavior(typeof(TransactionBehavior<,>)); cfg.AddOpenBehavior(typeof(TransactionBehavior<,>));
}); });
// Register the command validators for the validator behavior (validators based on FluentValidation library) // Register the command validators for the validator behavior (validators based on FluentValidation library)
services.AddSingleton<IValidator<CancelOrderCommand>, CancelOrderCommandValidator>(); services.AddSingleton<IValidator<CancelOrderCommand>, CancelOrderCommandValidator>();
services.AddSingleton<IValidator<CreateOrderCommand>, CreateOrderCommandValidator>(); services.AddSingleton<IValidator<CreateOrderCommand>, CreateOrderCommandValidator>();
services.AddSingleton<IValidator<IdentifiedCommand<CreateOrderCommand, bool>>, IdentifiedCommandValidator>(); services.AddSingleton<IValidator<IdentifiedCommand<CreateOrderCommand, bool>>, IdentifiedCommandValidator>();
services.AddSingleton<IValidator<ShipOrderCommand>, ShipOrderCommandValidator>(); services.AddSingleton<IValidator<ShipOrderCommand>, ShipOrderCommandValidator>();
/*
// Build the MediatR pipeline
services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(ValidatorBehavior<,>));
services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(TransactionBehavior<,>));
*/
var queriesConnectionString = builder.Configuration["ConnectionString"]; var queriesConnectionString = builder.Configuration["ConnectionString"];
@ -78,19 +71,17 @@ services.AddSingleton<IIntegrationEventHandler<OrderStockRejectedIntegrationEven
services.AddSingleton<IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>, UserCheckoutAcceptedIntegrationEventHandler>(); services.AddSingleton<IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>, UserCheckoutAcceptedIntegrationEventHandler>();
var app = builder.Build(); var app = builder.Build();
if (app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{ {
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
} }
var pathBase = app.Configuration["PATH_BASE"]; var pathBase = app.Configuration["PATH_BASE"];
if (!string.IsNullOrEmpty(pathBase)) if (!string.IsNullOrEmpty(pathBase))
{ {
app.UsePathBase(pathBase); app.UsePathBase(pathBase);
} }
app.UseSwagger().UseSwaggerUI(c => app.UseSwagger().UseSwaggerUI(c =>
{ {
c.SwaggerEndpoint($"{(!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty)}/swagger/v1/swagger.json", "Ordering.API V1"); 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) public static IServiceCollection AddCustomConfiguration(this IServiceCollection services, IConfiguration configuration)
{ {
services.AddOptions();
services.Configure<OrderingSettings>(configuration); services.Configure<OrderingSettings>(configuration);
services.Configure<ApiBehaviorOptions>(options => services.Configure<ApiBehaviorOptions>(options =>
{ {

View File

@ -9,18 +9,14 @@ builder.Configuration.AddJsonFile($"appsettings.{builder.Environment.Environment
builder.Configuration.AddEnvironmentVariables(); builder.Configuration.AddEnvironmentVariables();
builder.Services.AddCustomHealthCheck(builder.Configuration) builder.Services.AddCustomHealthCheck(builder.Configuration)
.Configure<BackgroundTaskSettings>(builder.Configuration) .Configure<BackgroundTaskSettings>(builder.Configuration)
.AddOptions()
.AddHostedService<GracePeriodManagerService>() .AddHostedService<GracePeriodManagerService>()
.AddEventBus(builder.Configuration); .AddEventBus(builder.Configuration);
var app = builder.Build(); var app = builder.Build();
if (app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{ {
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
} }
app.UseRouting(); app.UseRouting();
app.MapHealthChecks("/hc", new HealthCheckOptions() app.MapHealthChecks("/hc", new HealthCheckOptions()

View File

@ -85,19 +85,17 @@ builder.Services.AddSingleton<IIntegrationEventHandler<OrderStatusChangedToStock
builder.Services.AddSingleton<IIntegrationEventHandler<OrderStatusChangedToSubmittedIntegrationEvent>, OrderStatusChangedToSubmittedIntegrationEventHandler>(); builder.Services.AddSingleton<IIntegrationEventHandler<OrderStatusChangedToSubmittedIntegrationEvent>, OrderStatusChangedToSubmittedIntegrationEventHandler>();
var app = builder.Build(); var app = builder.Build();
if (app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{ {
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
} }
var pathBase = builder.Configuration["PATH_BASE"]; var pathBase = builder.Configuration["PATH_BASE"];
if (!string.IsNullOrEmpty(pathBase)) if (!string.IsNullOrEmpty(pathBase))
{ {
app.UsePathBase(pathBase); app.UsePathBase(pathBase);
} }
app.UseRouting(); app.UseRouting();
app.UseCors("CorsPolicy"); app.UseCors("CorsPolicy");
app.UseAuthentication(); app.UseAuthentication();

View File

@ -57,19 +57,17 @@ else
} }
RegisterEventBus(builder.Services); RegisterEventBus(builder.Services);
var app = builder.Build(); var app = builder.Build();
if (app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{ {
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
} }
var pathBase = app.Configuration["PATH_BASE"]; var pathBase = app.Configuration["PATH_BASE"];
if (!string.IsNullOrEmpty(pathBase)) if (!string.IsNullOrEmpty(pathBase))
{ {
app.UsePathBase(pathBase); app.UsePathBase(pathBase);
} }
ConfigureEventBus(app); ConfigureEventBus(app);
app.UseRouting(); app.UseRouting();

View File

@ -12,11 +12,7 @@ builder.WebHost.CaptureStartupErrors(false);
var app = builder.Build(); var app = builder.Build();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub"); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub");
if (app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{ {
app.UseExceptionHandler("/Error"); app.UseExceptionHandler("/Error");
} }
@ -72,8 +68,7 @@ static void AddHealthChecks(WebApplicationBuilder builder)
static void AddCustomMvc(WebApplicationBuilder builder) static void AddCustomMvc(WebApplicationBuilder builder)
{ {
builder.Services.AddOptions() builder.Services.Configure<AppSettings>(builder.Configuration)
.Configure<AppSettings>(builder.Configuration)
.AddSession() .AddSession()
.AddDistributedMemoryCache(); .AddDistributedMemoryCache();

View File

@ -40,11 +40,6 @@ builder.Logging.AddAzureWebAppDiagnostics();
var app = builder.Build(); 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 // 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() // 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<IAntiforgery>(); var antiForgery = app.Services.GetRequiredService<IAntiforgery>();

View File

@ -12,7 +12,6 @@ builder.WebHost.CaptureStartupErrors(false);
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration); builder.Services.AddApplicationInsightsTelemetry(builder.Configuration);
builder.Services.AddApplicationInsightsKubernetesEnricher(); builder.Services.AddApplicationInsightsKubernetesEnricher();
builder.Services.AddMvc(); builder.Services.AddMvc();
builder.Services.AddOptions();
builder.Services.AddHealthChecks() builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy()); .AddCheck("self", () => HealthCheckResult.Healthy());
builder.Services builder.Services
@ -21,11 +20,7 @@ builder.Services
var app = builder.Build(); var app = builder.Build();
if (app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{ {
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
} }

View File

@ -1,4 +1,4 @@
namespace WebhookClient; namespace WebhookClient;
public class Startup public class Startup
{ {
@ -36,15 +36,12 @@ public class Startup
app.UsePathBase(pathBase); app.UsePathBase(pathBase);
} }
if (env.IsDevelopment()) if (!env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{ {
app.UseExceptionHandler("/Error"); 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. // 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 => app.Map("/check", capp =>
{ {
capp.Run(async (context) => capp.Run(async (context) =>
@ -97,7 +94,6 @@ static class ServiceExtensions
{ {
public static IServiceCollection AddConfiguration(this IServiceCollection services, IConfiguration configuration) public static IServiceCollection AddConfiguration(this IServiceCollection services, IConfiguration configuration)
{ {
services.AddOptions();
services.Configure<Settings>(configuration); services.Configure<Settings>(configuration);
return services; return services;
} }