Skip to content

Commit

Permalink
Adds Aspire (#859)
Browse files Browse the repository at this point in the history
* Added aspire projects

* Added Aspire test project

* Renamend aspire tests file

* Final commit for adding aspire
  • Loading branch information
benjaminmishra authored Oct 30, 2024
1 parent 92a5f22 commit bed60d8
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 14 deletions.
84 changes: 70 additions & 14 deletions .template.config/template.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,72 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Steve Smith @ardalis, Erik Dahl",
"classifications": [
"Web","ASP.NET","Clean Architecture"
],
"tags": {
"$schema": "http://json.schemastore.org/template",
"author": "Steve Smith @ardalis, Erik Dahl",
"classifications": [
"Web",
"ASP.NET",
"Clean Architecture"
],
"tags": {
"language": "C#",
"type":"project"
},
"identity": "Ardalis.CleanArchitecture.Template",
"name": "ASP.NET Clean Architecture Solution",
"shortName": "clean-arch",
"sourceName": "Clean.Architecture",
"preferNameDirectory": true
}
"type": "project"
},
"identity": "Ardalis.CleanArchitecture.Template",
"name": "ASP.NET Clean Architecture Solution",
"shortName": "clean-arch",
"sourceName": "Clean.Architecture",
"preferNameDirectory": true,
"symbols": {
"aspire": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"description": "Include .NET Aspire."
}
},
"sources": [
{
"modifiers": [
{
"condition": "(!aspire)",
"exclude": [
"src/Clean.Architecture.AspireHost/**",
"src/Clean.Architecture.ServiceDefaults/**",
"tests/Clean.Architecture.AspireTests/**"
]
},
{
"condition": "(aspire)",
"include": [
"src/Clean.Architecture.AspireHost/**",
"src/Clean.Architecture.ServiceDefaults/**",
"tests/Clean.Architecture.AspireTests/**"
]
}
]
}
],
"primaryOutputs": [{
"path": "src/Clean.Architecture.AspireHost/Clean.Architecture.AspireHost.csproj"
},
{
"path": "src/Clean.Architecture.ServiceDefaults/Clean.Architecture.ServiceDefaults.csproj"
},
{
"path": "tests/Clean.Architecture.AspireTests/Clean.Architecture.AspireTests.csproj"
}
],
"postActions": [{
"condition": "(aspire)",
"description": "Add Aspire projects to solution",
"manualInstructions": [{
"text": "Add generated project to solution manually."
}
],
"args": {
"isRoot": "true"
},
"actionId": "D396686C-DE0E-4DE6-906D-291CD29FC5DE",
"continueOnError": true
}
]
}
1 change: 1 addition & 0 deletions Clean.Architecture.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
CleanArchitecture.nuspec = CleanArchitecture.nuspec
Directory.Build.props = Directory.Build.props
Directory.Packages.props = Directory.Packages.props
template.json = template.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Clean.Architecture.FunctionalTests", "tests\Clean.Architecture.FunctionalTests\Clean.Architecture.FunctionalTests.csproj", "{7D84EFEE-A7D9-44AD-A0A3-38EC7882D94C}"
Expand Down
11 changes: 11 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,16 @@
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0-pre.35" />
<!--#if (aspire) -->
<PackageVersion Include="Aspire.Hosting.AppHost" Version="9.0.0-rc.1.24511.1" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="9.0.0-rc.1.24511.1" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.9.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" />
<PackageVersion Include="Aspire.Hosting.Testing" Version="9.0.0-rc.1.24511.1" />
<!--#endif -->
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>c540eeb6-e06b-4456-a539-be58dd8b88c7</UserSecretsId>
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\Clean.Architecture.Web\Clean.Architecture.Web.csproj" />
</ItemGroup>
<ItemGroup>
<!-- The IsAspireProjectResource attribute tells .NET Aspire to treat this
reference as a standard project reference and not attempt to generate
a metadata file -->
<ProjectReference Include="..\Clean.Architecture.ServiceDefaults\Clean.Architecture.ServiceDefaults.csproj"
IsAspireProjectResource="false" />
</ItemGroup>

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

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

builder.Build().Run();
29 changes: 29 additions & 0 deletions src/Clean.Architecture.AspireHost/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:17143;http://localhost:15258",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21007",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22245"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15258",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19187",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20134"
}
}
}
}
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/Clean.Architecture.AspireHost/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"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</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>
118 changes: 118 additions & 0 deletions src/Clean.Architecture.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
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.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;
}
}
3 changes: 3 additions & 0 deletions src/Clean.Architecture.Web/Clean.Architecture.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<ItemGroup>
<ProjectReference Include="..\Clean.Architecture.Infrastructure\Clean.Architecture.Infrastructure.csproj" />
<ProjectReference Include="..\Clean.Architecture.UseCases\Clean.Architecture.UseCases.csproj" />
<!--#if (aspire)-->
<ProjectReference Include="..\Clean.Architecture.ServiceDefaults\Clean.Architecture.ServiceDefaults.csproj" />
<!--#endif -->
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions src/Clean.Architecture.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
o.ShortSchemaNames = true;
});

#if (aspire)
builder.AddServiceDefaults();
#endif

var app = builder.Build();

await app.UseAppMiddleware();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Clean.Architecture.AspireTests.Tests;

public class AspireIntegrationTests
{
// Follow the link below to write you tests with Aspire
// https://learn.microsoft.com/en-us/dotnet/aspire/testing/write-your-first-test?pivots=xunit
}
Loading

0 comments on commit bed60d8

Please sign in to comment.