From f970d7b3d8e6d486b7942d45c5621e52c3f7dffe Mon Sep 17 00:00:00 2001 From: Robin-Manuel Thiel Date: Sun, 10 Dec 2023 17:34:05 +0100 Subject: [PATCH 1/2] Update Readme Docs --- README.md | 37 ++++++++++++++++--- .../Monitoring/MonitoringExtensions.cs | 1 + 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1bc9d42..b0b9344 100644 --- a/README.md +++ b/README.md @@ -35,29 +35,55 @@ public Startup(IConfiguration configuration) } ``` -Add the `StartupOptions` and register the default setup. +Add the `StartupOptions` and register the default setup using the `AddDefaultSetup` extension method. Alternatively, you can also register our defaults manually in case you need to tweak in some of your own adjustments. ```csharp public void ConfigureServices(IServiceCollection services) { + // Register all options automatically services.AddDefaultSetup(_options); - // ... + // or + + // Register the options manually + services.AddDefaultCors(); + services.AddDefaultSwagger(_options.OpenApiEnvironment); + services.AddDefaultMonitoring(_options.MonitoringEnvironment); + services.AddDefaultControllers(_options.DaprEnvironment != null, _options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes); + services.AddDefaultHealthChecks(_options); + services.AddDefaultRouting(); } ``` -Make sure, the default setup gets used. +Make sure, the default setup is getting used. Again, you can either use the `UseDefaultSetup` extension method or use the options manually, in case you need to tweak in some of your own adjustments. + +```csharp ```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseDefaultSetup(env, _options); - // ... + // or + + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseDefaultCors(); + app.UseDefaultSwagger(_options.OpenApiEnvironment); + app.UseDefaultMonitoring(_options.MonitoringEnvironment); + app.UseDefaultRouting(); + app.UseCloudEvents(); // when using Dapr + app.UseAuthentication(); + app.UseAuthorization(); + app.UseErrorHandlerMiddleware(); + app.UseDefaultEndpoints(_options); } ``` -For modern or Minimal API services, this would look similar to this. In the `Program.cs`, add the `StartupOptions` and register the default setup. +For modern or Minimal API services (introduced in .NET 6), this would look similar to this. In the `Program.cs`, add the `StartupOptions` and register the default setup. ```csharp var options = new StartupOptions(); @@ -70,6 +96,7 @@ options // ... +app.AddDefaultSetup(options); app.UseDefaultSetup(app.Environment, options); ``` diff --git a/src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs b/src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs index f31651a..62e19af 100644 --- a/src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs +++ b/src/Wemogy.AspNet/Monitoring/MonitoringExtensions.cs @@ -56,6 +56,7 @@ public static IServiceCollection AddDefaultMonitoring( } }); + // Azure if (environment.UseApplicationInsights) { services.AddOpenTelemetry().UseAzureMonitor(options => From 4e8ba071bebfd1bd58882cbdb27471a6ee285de6 Mon Sep 17 00:00:00 2001 From: Robin-Manuel Thiel Date: Sun, 10 Dec 2023 22:05:04 +0100 Subject: [PATCH 2/2] Make Application Insights Optional --- .../Startup/StartupExtensions.cs | 31 +++++++++---------- src/Wemogy.AspNet/Wemogy.AspNet.csproj | 2 +- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/Wemogy.AspNet/Startup/StartupExtensions.cs b/src/Wemogy.AspNet/Startup/StartupExtensions.cs index 275bbdf..a6a43dc 100644 --- a/src/Wemogy.AspNet/Startup/StartupExtensions.cs +++ b/src/Wemogy.AspNet/Startup/StartupExtensions.cs @@ -56,9 +56,7 @@ public static void AddDefaultSetup(this IServiceCollection serviceCollection, St serviceCollection.AddDefaultControllers(options.DaprEnvironment != null, options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes); - // Health checks - var healthChecksBuilder = serviceCollection.AddHealthChecks(); - options.ConfigureHealthCheckBuilder?.Invoke(healthChecksBuilder); + serviceCollection.AddDefaultHealthChecks(options); serviceCollection.AddDefaultRouting(); } @@ -77,6 +75,12 @@ public static void AddDefaultCors(this IServiceCollection serviceCollection) }); } + public static void AddDefaultHealthChecks(this IServiceCollection serviceCollection, StartupOptions options) + { + var healthChecksBuilder = serviceCollection.AddHealthChecks(); + options.ConfigureHealthCheckBuilder?.Invoke(healthChecksBuilder); + } + public static void AddDefaultRouting(this IServiceCollection serviceCollection) { // Enforce lowercase routes @@ -120,19 +124,7 @@ public static void UseDefaultSetup(this IApplicationBuilder applicationBuilder, applicationBuilder.UseMiddleware(middleware); } - if (options.DaprEnvironment != null) - { - applicationBuilder.UseEndpoints(endpoints => - { - endpoints.MapSubscribeHandler(); // Register endpoints for Dapr PubSub - endpoints.MapControllers(); - endpoints.MapHealthChecks("/healthz"); - }); - } - else - { - applicationBuilder.UseDefaultEndpoints(); - } + applicationBuilder.UseDefaultEndpoints(options); } public static void UseDefaultRouting(this IApplicationBuilder applicationBuilder) @@ -145,12 +137,17 @@ public static void UseDefaultCors(this IApplicationBuilder applicationBuilder) applicationBuilder.UseCors(); } - public static void UseDefaultEndpoints(this IApplicationBuilder applicationBuilder) + public static void UseDefaultEndpoints(this IApplicationBuilder applicationBuilder, StartupOptions options) { applicationBuilder.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapHealthChecks("/healthz"); + + if (options.DaprEnvironment != null) + { + endpoints.MapSubscribeHandler(); // Register endpoints for Dapr PubSub + } }); } diff --git a/src/Wemogy.AspNet/Wemogy.AspNet.csproj b/src/Wemogy.AspNet/Wemogy.AspNet.csproj index c0e9fa9..42bb513 100644 --- a/src/Wemogy.AspNet/Wemogy.AspNet.csproj +++ b/src/Wemogy.AspNet/Wemogy.AspNet.csproj @@ -42,7 +42,7 @@ - +