Skip to content

Commit

Permalink
✨ Add support for .NET Aspire
Browse files Browse the repository at this point in the history
  • Loading branch information
jasontaylordev committed Nov 28, 2024
1 parent 1b0aafd commit ea5fd63
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .template.config/dotnetcli.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
},
"UseSQLite": {
"longName": "use-sqlite"
},
"UseAspire": {
"longName": "use-aspire"
}
}
}
20 changes: 20 additions & 0 deletions .template.config/ide.host.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@
"text": "Use SQLite for database (default is LocalDB)."
},
"isVisible": true
},
{
"id": "PipelineProvider",
"name": {
"text": "Pipeline Provider"
},
"description": {
"text": "Select the pipeline provider."
},
"isVisible": true
},
{
"id": "UseAspire",
"name": {
"text": "Use Aspire"
},
"description": {
"text": "Use .NET Aspire"
},
"isVisible": true
}
]
}
13 changes: 13 additions & 0 deletions .template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
"defaultValue": "false",
"description": "Whether to use SQLite instead of LocalDB."
},
"UseAspire": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"description": "Use .NET Aspire"
},
"PipelineProvider": {
"type": "parameter",
"datatype": "choice",
Expand Down Expand Up @@ -192,6 +198,13 @@
"appsettings.SQLite.json": "appsettings.json",
"TestDatabase.Sqlite.cs": "TestDatabase.cs"
}
},
{
"condition": "(!UseAspire)",
"exclude": [
"src/AppHost/**",
"src/ServiceDefaults/**"
]
}
]
}
Expand Down
24 changes: 22 additions & 2 deletions CleanArchitecture.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Application.FunctionalTests
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Infrastructure.IntegrationTests", "tests\Infrastructure.IntegrationTests\Infrastructure.IntegrationTests.csproj", "{01FA6786-921D-4CE8-8C50-4FDA66C9477D}"
EndProject
#if (UseAspire)
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppHost", "src\AppHost\AppHost.csproj", "{36483B25-E165-46F3-AA1C-ADE75A46BD8E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceDefaults", "src\ServiceDefaults\ServiceDefaults.csproj", "{15AFAC17-7E6E-4DB9-A5D9-EA96C5287CD2}"
EndProject
#endif
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -67,12 +73,12 @@ Global
{4E4EE20C-F06A-4A1B-851F-C5577796941C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4E4EE20C-F06A-4A1B-851F-C5577796941C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4E4EE20C-F06A-4A1B-851F-C5577796941C}.Release|Any CPU.Build.0 = Release|Any CPU
#if (!UseApiOnly)
#if (!UseApiOnly)
{21334974-6B07-47BB-B664-AC62FE58C12B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21334974-6B07-47BB-B664-AC62FE58C12B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21334974-6B07-47BB-B664-AC62FE58C12B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21334974-6B07-47BB-B664-AC62FE58C12B}.Release|Any CPU.Build.0 = Release|Any CPU
#endif
#endif
{EA6127A5-94C9-4C31-AD11-E6811B92B520}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA6127A5-94C9-4C31-AD11-E6811B92B520}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA6127A5-94C9-4C31-AD11-E6811B92B520}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -81,6 +87,16 @@ Global
{01FA6786-921D-4CE8-8C50-4FDA66C9477D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01FA6786-921D-4CE8-8C50-4FDA66C9477D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01FA6786-921D-4CE8-8C50-4FDA66C9477D}.Release|Any CPU.Build.0 = Release|Any CPU
#if (UseAspire)
{36483B25-E165-46F3-AA1C-ADE75A46BD8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{36483B25-E165-46F3-AA1C-ADE75A46BD8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{36483B25-E165-46F3-AA1C-ADE75A46BD8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{36483B25-E165-46F3-AA1C-ADE75A46BD8E}.Release|Any CPU.Build.0 = Release|Any CPU
{15AFAC17-7E6E-4DB9-A5D9-EA96C5287CD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15AFAC17-7E6E-4DB9-A5D9-EA96C5287CD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15AFAC17-7E6E-4DB9-A5D9-EA96C5287CD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15AFAC17-7E6E-4DB9-A5D9-EA96C5287CD2}.Release|Any CPU.Build.0 = Release|Any CPU
#endif
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -97,6 +113,10 @@ Global
#endif
{EA6127A5-94C9-4C31-AD11-E6811B92B520} = {664D406C-2F83-48F0-BFC3-408D5CB53C65}
{01FA6786-921D-4CE8-8C50-4FDA66C9477D} = {664D406C-2F83-48F0-BFC3-408D5CB53C65}
#if (UseAspire)
{36483B25-E165-46F3-AA1C-ADE75A46BD8E} = {6ED356A7-8B47-4613-AD01-C85CF28491BD}
{15AFAC17-7E6E-4DB9-A5D9-EA96C5287CD2} = {6ED356A7-8B47-4613-AD01-C85CF28491BD}
#endif
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3CB609D9-5D54-4C11-A371-DAAC8B74E430}
Expand Down
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<WarningsNotAsErrors>NU1901;NU1902;NU1903;NU1904</WarningsNotAsErrors>
<!-- <ArtifactsPath>$(MSBuildThisFileDirectory)artifacts</ArtifactsPath> -->
<ImplicitUsings>enable</ImplicitUsings>
<DefineConstants>UseAspire</DefineConstants>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
12 changes: 11 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@
<MicrosoftExtensionsVersion>9.0.0</MicrosoftExtensionsVersion>
</PropertyGroup>
<ItemGroup>
<!-- Begin Aspire -->
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.10.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.10.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<!-- End Aspire -->
<PackageVersion Include="Ardalis.GuardClauses" Version="4.6.0" />
<PackageVersion Include="AutoMapper" Version="13.0.1" />
<PackageVersion Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.2" />
<PackageVersion Include="Azure.Identity" Version="1.13.1" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="FluentAssertions" Version="6.12.2" />
<PackageVersion Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.10.0" />
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
<PackageVersion Include="MediatR" Version="12.4.1" />
<PackageVersion Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="$(AspnetVersion)" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="$(AspnetVersion)" />
Expand Down
23 changes: 23 additions & 0 deletions src/AppHost/AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<RootNamespace>CleanArchitecture.AppHost</RootNamespace>
<AssemblyName>CleanArchitecture.AppHost</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Web\Web.csproj" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var builder = DistributedApplication.CreateBuilder(args);

builder.AddProject<Projects.Web>("web");

builder.Build().Run();
29 changes: 29 additions & 0 deletions src/AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17078;http://localhost:15010",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21118",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22071"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15010",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19152",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20152"
}
}
}
}
8 changes: 8 additions & 0 deletions src/AppHost/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions src/AppHost/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
119 changes: 119 additions & 0 deletions src/ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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 .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 TBuilder AddServiceDefaults<TBuilder>(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<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });

return builder;
}

public static TBuilder ConfigureOpenTelemetry<TBuilder>(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()
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static TBuilder AddOpenTelemetryExporters<TBuilder>(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<TBuilder>(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/dotnet/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("/health");

// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks("/alive", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});
}

return app;
}
}
24 changes: 24 additions & 0 deletions src/ServiceDefaults/ServiceDefaults.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
<RootNamespace>CleanArchitecture.ServiceDefaults</RootNamespace>
<AssemblyName>CleanArchitecture.ServiceDefaults</AssemblyName>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
</ItemGroup>

</Project>
Loading

0 comments on commit ea5fd63

Please sign in to comment.