From 4d541fe9cdfcc7287d70c1a1962e3ba259a42621 Mon Sep 17 00:00:00 2001 From: Gulum Date: Fri, 5 Jun 2026 23:35:15 +0200 Subject: [PATCH] ARCH-2: add Aspire AppHost + ServiceDefaults (Aspire 13.4.2) - New projects GerbilManager.AppHost / GerbilManager.ServiceDefaults (templates) - Added both to GerbilManager.slnx - WebAPI references ServiceDefaults; AddServiceDefaults + MapDefaultEndpoints wired Co-Authored-By: Claude Opus 4.8 (1M context) --- GerbilManager.AppHost/AppHost.cs | 3 + .../GerbilManager.AppHost.csproj | 11 ++ .../Properties/launchSettings.json | 29 ++++ .../appsettings.Development.json | 8 ++ GerbilManager.AppHost/appsettings.json | 9 ++ GerbilManager.AppHost/aspire.config.json | 5 + GerbilManager.ServiceDefaults/Extensions.cs | 127 ++++++++++++++++++ .../GerbilManager.ServiceDefaults.csproj | 22 +++ GerbilManager.slnx | 2 + .../GerbilManagerWebAPI.csproj | 4 + GerbilManagerWebAPI/Program.cs | 4 + 11 files changed, 224 insertions(+) create mode 100644 GerbilManager.AppHost/AppHost.cs create mode 100644 GerbilManager.AppHost/GerbilManager.AppHost.csproj create mode 100644 GerbilManager.AppHost/Properties/launchSettings.json create mode 100644 GerbilManager.AppHost/appsettings.Development.json create mode 100644 GerbilManager.AppHost/appsettings.json create mode 100644 GerbilManager.AppHost/aspire.config.json create mode 100644 GerbilManager.ServiceDefaults/Extensions.cs create mode 100644 GerbilManager.ServiceDefaults/GerbilManager.ServiceDefaults.csproj diff --git a/GerbilManager.AppHost/AppHost.cs b/GerbilManager.AppHost/AppHost.cs new file mode 100644 index 0000000..97422e2 --- /dev/null +++ b/GerbilManager.AppHost/AppHost.cs @@ -0,0 +1,3 @@ +var builder = DistributedApplication.CreateBuilder(args); + +builder.Build().Run(); diff --git a/GerbilManager.AppHost/GerbilManager.AppHost.csproj b/GerbilManager.AppHost/GerbilManager.AppHost.csproj new file mode 100644 index 0000000..9300699 --- /dev/null +++ b/GerbilManager.AppHost/GerbilManager.AppHost.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + enable + enable + b153f50c-bec1-401e-9893-f9df7cff2ebd + + + diff --git a/GerbilManager.AppHost/Properties/launchSettings.json b/GerbilManager.AppHost/Properties/launchSettings.json new file mode 100644 index 0000000..e6cad5e --- /dev/null +++ b/GerbilManager.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17004;http://localhost:15265", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21284", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22112" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15265", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19171", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20259" + } + } + } +} diff --git a/GerbilManager.AppHost/appsettings.Development.json b/GerbilManager.AppHost/appsettings.Development.json new file mode 100644 index 0000000..ff66ba6 --- /dev/null +++ b/GerbilManager.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/GerbilManager.AppHost/appsettings.json b/GerbilManager.AppHost/appsettings.json new file mode 100644 index 0000000..2185f95 --- /dev/null +++ b/GerbilManager.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/GerbilManager.AppHost/aspire.config.json b/GerbilManager.AppHost/aspire.config.json new file mode 100644 index 0000000..dd163e4 --- /dev/null +++ b/GerbilManager.AppHost/aspire.config.json @@ -0,0 +1,5 @@ +{ + "appHost": { + "path": "GerbilManager.AppHost.csproj" + } +} diff --git a/GerbilManager.ServiceDefaults/Extensions.cs b/GerbilManager.ServiceDefaults/Extensions.cs new file mode 100644 index 0000000..97642a1 --- /dev/null +++ b/GerbilManager.ServiceDefaults/Extensions.cs @@ -0,0 +1,127 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ServiceDiscovery; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +// Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry. +// This project should be referenced by each service project in your solution. +// To learn more about using this project, see https://aka.ms/aspire/service-defaults +public static class Extensions +{ + private const string HealthEndpointPath = "/health"; + private const string AlivenessEndpointPath = "/alive"; + + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + // Turn on resilience by default + http.AddStandardResilienceHandler(); + + // Turn on service discovery by default + http.AddServiceDiscovery(); + }); + + // Uncomment the following to restrict the allowed schemes for service discovery. + // builder.Services.Configure(options => + // { + // options.AllowedSchemes = ["https"]; + // }); + + return builder; + } + + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation(tracing => + // Exclude health check requests from tracing + tracing.Filter = context => + !context.Request.Path.StartsWithSegments(HealthEndpointPath) + && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) + ) + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) + //{ + // builder.Services.AddOpenTelemetry() + // .UseAzureMonitor(); + //} + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/aspire/healthchecks for details before enabling these endpoints in non-development environments. + if (app.Environment.IsDevelopment()) + { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks(HealthEndpointPath); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/GerbilManager.ServiceDefaults/GerbilManager.ServiceDefaults.csproj b/GerbilManager.ServiceDefaults/GerbilManager.ServiceDefaults.csproj new file mode 100644 index 0000000..1efc067 --- /dev/null +++ b/GerbilManager.ServiceDefaults/GerbilManager.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net10.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/GerbilManager.slnx b/GerbilManager.slnx index e412729..763646b 100644 --- a/GerbilManager.slnx +++ b/GerbilManager.slnx @@ -1,3 +1,5 @@ + + diff --git a/GerbilManagerWebAPI/GerbilManagerWebAPI.csproj b/GerbilManagerWebAPI/GerbilManagerWebAPI.csproj index e37ad6c..7897860 100644 --- a/GerbilManagerWebAPI/GerbilManagerWebAPI.csproj +++ b/GerbilManagerWebAPI/GerbilManagerWebAPI.csproj @@ -17,4 +17,8 @@ + + + + diff --git a/GerbilManagerWebAPI/Program.cs b/GerbilManagerWebAPI/Program.cs index fa5ac49..2e5a64d 100644 --- a/GerbilManagerWebAPI/Program.cs +++ b/GerbilManagerWebAPI/Program.cs @@ -3,6 +3,8 @@ using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); +builder.AddServiceDefaults(); + // Add services to the container. builder.Services.AddControllers(); @@ -14,6 +16,8 @@ builder.Services.AddScoped(); var app = builder.Build(); +app.MapDefaultEndpoints(); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) {