0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-06-30 02:31:08 +00:00

feat: Cleanup redundant configurations

This commit is contained in:
alex289 2024-11-14 14:29:03 +01:00
parent cfe5d029aa
commit 2a048b682a
No known key found for this signature in database
GPG Key ID: 573F77CD2D87F863
7 changed files with 54 additions and 72 deletions

View File

@ -6,6 +6,7 @@ using CleanArchitecture.Domain.Extensions;
using CleanArchitecture.Domain.Rabbitmq.Extensions; using CleanArchitecture.Domain.Rabbitmq.Extensions;
using CleanArchitecture.Infrastructure.Database; using CleanArchitecture.Infrastructure.Database;
using CleanArchitecture.Infrastructure.Extensions; using CleanArchitecture.Infrastructure.Extensions;
using CleanArchitecture.ServiceDefaults;
using HealthChecks.ApplicationStatus.DependencyInjection; using HealthChecks.ApplicationStatus.DependencyInjection;
using HealthChecks.UI.Client; using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;

View File

@ -1,13 +1,5 @@
{ {
"$schema": "https://json.schemastore.org/launchsettings.json", "$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:38452",
"sslPort": 44309
}
},
"profiles": { "profiles": {
"CleanArchitecture.Api": { "CleanArchitecture.Api": {
"commandName": "Project", "commandName": "Project",
@ -18,14 +10,6 @@
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
} }
} }
} }

View File

@ -13,6 +13,9 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" /> <PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.RabbitMQ" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.Redis" Version="9.0.0" />
<PackageReference Include="Aspire.Hosting.SqlServer" Version="9.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,5 +1,26 @@
var builder = DistributedApplication.CreateBuilder(args); var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.CleanArchitecture_Api>("cleanarchitecture-api"); var redis = builder.AddRedis("Redis");
var rabbitPasswordRessource = new ParameterResource("password", _ => "guest");
var rabbitPasswordParameter =
builder.AddParameter("username", rabbitPasswordRessource.Value);
var rabbitMq = builder
.AddRabbitMQ("RabbitMq", null, rabbitPasswordParameter, 5672)
.WithManagementPlugin();
var sqlServer = builder.AddSqlServer("SqlServer");
builder.AddProject<Projects.CleanArchitecture_Api>("CleanArchitecture.Api")
.WithHttpsEndpoint(17270)
.WithHealthCheck("Api Health")
.WithOtlpExporter()
.WithReference(redis)
.WaitFor(redis)
.WithReference(rabbitMq)
.WaitFor(rabbitMq)
.WithReference(sqlServer)
.WaitFor(sqlServer);
builder.Build().Run(); builder.Build().Run();

View File

@ -1,7 +1,7 @@
{ {
"$schema": "https://json.schemastore.org/launchsettings.json", "$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": { "profiles": {
"https": { "Aspire": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
@ -10,19 +10,8 @@
"ASPNETCORE_ENVIRONMENT": "Development", "ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development", "DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21200", "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21200",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22111" "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22111",
} "ASPIRE_ENABLED": "true"
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15188",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19170",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20198"
} }
} }
} }

View File

@ -15,6 +15,7 @@
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" /> <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" /> <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" /> <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.9.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" /> <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" /> <PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
</ItemGroup> </ItemGroup>

View File

@ -2,21 +2,26 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ServiceDiscovery; using Microsoft.Extensions.ServiceDiscovery;
using OpenTelemetry; using OpenTelemetry;
using OpenTelemetry.Metrics; using OpenTelemetry.Metrics;
using OpenTelemetry.Trace; using OpenTelemetry.Trace;
namespace Microsoft.Extensions.Hosting; namespace CleanArchitecture.ServiceDefaults;
// Adds common .NET 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/dotnet/aspire/service-defaults
public static class Extensions public static class Extensions
{ {
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder private const string AspireEnabled = "ASPIRE_ENABLED";
public static void AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{ {
if (builder.Configuration[AspireEnabled] != "true")
{
return;
}
builder.ConfigureOpenTelemetry(); builder.ConfigureOpenTelemetry();
builder.AddDefaultHealthChecks(); builder.AddDefaultHealthChecks();
@ -25,23 +30,17 @@ public static class Extensions
builder.Services.ConfigureHttpClientDefaults(http => builder.Services.ConfigureHttpClientDefaults(http =>
{ {
// Turn on resilience by default
http.AddStandardResilienceHandler(); http.AddStandardResilienceHandler();
// Turn on service discovery by default
http.AddServiceDiscovery(); http.AddServiceDiscovery();
}); });
// Uncomment the following to restrict the allowed schemes for service discovery. builder.Services.Configure<ServiceDiscoveryOptions>(options =>
// builder.Services.Configure<ServiceDiscoveryOptions>(options => {
// { options.AllowedSchemes = ["https"];
// options.AllowedSchemes = ["https"]; });
// });
return builder;
} }
public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder private static void ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{ {
builder.Logging.AddOpenTelemetry(logging => builder.Logging.AddOpenTelemetry(logging =>
{ {
@ -60,17 +59,14 @@ public static class Extensions
{ {
tracing.AddSource(builder.Environment.ApplicationName) tracing.AddSource(builder.Environment.ApplicationName)
.AddAspNetCoreInstrumentation() .AddAspNetCoreInstrumentation()
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) .AddGrpcClientInstrumentation()
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation(); .AddHttpClientInstrumentation();
}); });
builder.AddOpenTelemetryExporters(); builder.AddOpenTelemetryExporters();
return builder;
} }
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder private static void AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{ {
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
@ -78,42 +74,29 @@ public static class Extensions
{ {
builder.Services.AddOpenTelemetry().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<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder private static void AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{ {
builder.Services.AddHealthChecks() builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
return builder;
} }
public static WebApplication MapDefaultEndpoints(this WebApplication app) public static void MapDefaultEndpoints(this WebApplication app)
{ {
// Adding health checks endpoints to applications in non-development environments has security implications. if (app.Configuration[AspireEnabled] != "true")
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. {
return;
}
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks("/health"); app.MapHealthChecks("/health");
// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks("/alive", new HealthCheckOptions app.MapHealthChecks("/alive", new HealthCheckOptions
{ {
Predicate = r => r.Tags.Contains("live") Predicate = r => r.Tags.Contains("live")
}); });
} }
return app;
} }
} }