Skip to content

Commit ddcc81b

Browse files
RolandGuijtRoland Guijt
andauthored
Add Aspire.ServiceDefaults to slnx (v7 and v8) and apply Aspire to SessionManagement sample in v8. (#315)
* Add Aspire.ServiceDefaults to slnx (v7 and v8) and apply Aspire to SessionManagement sample in v8. * Fix naming problem with folder for Aspire.ServiceDefaults project * Fix references * Fix references * Fix 2x slnx project references --------- Co-authored-by: Roland Guijt <roland.guijt@duendesoftware.com>
1 parent 01e9a2e commit ddcc81b

22 files changed

Lines changed: 285 additions & 38 deletions

File tree

IdentityServer/v7/Apire.ServiceDefaults/Aspire.ServiceDefaults.csproj renamed to IdentityServer/v7/Aspire.ServiceDefaults/Aspire.ServiceDefaults.csproj

File renamed without changes.

IdentityServer/v7/Apire.ServiceDefaults/Extensions.cs renamed to IdentityServer/v7/Aspire.ServiceDefaults/Extensions.cs

File renamed without changes.

IdentityServer/v7/SessionManagement/Api/Api.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<ProjectReference Include="..\..\Apire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
15-
<ProjectReference Include="..\SessionManagement.ServiceDefaults\SessionManagement.ServiceDefaults.csproj" />
14+
<ProjectReference Include="..\..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
1615
</ItemGroup>
1716

1817
</Project>

IdentityServer/v7/SessionManagement/Client/Client.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515

1616
<ItemGroup>
17-
<ProjectReference Include="..\..\Apire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
18-
<ProjectReference Include="..\SessionManagement.ServiceDefaults\SessionManagement.ServiceDefaults.csproj" />
17+
<ProjectReference Include="..\..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
1918
</ItemGroup>
2019

2120
</Project>

IdentityServer/v7/SessionManagement/IdentityServerHost/IdentityServerHost.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13-
<ProjectReference Include="..\..\Apire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
14-
<ProjectReference Include="..\SessionManagement.ServiceDefaults\SessionManagement.ServiceDefaults.csproj" />
13+
<ProjectReference Include="..\..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
1514
</ItemGroup>
1615

1716
</Project>

IdentityServer/v7/SessionManagement/SessionManagement.sln

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityServerHost", "Ident
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SessionManagement.AppHost", "SessionManagement.AppHost\SessionManagement.AppHost.csproj", "{BD383672-7EF5-4737-91A9-92918CF2BBA7}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "..\Apire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{A614B4B7-AB24-421D-808F-C35D4139F8E6}"
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.ServiceDefaults", "..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj", "{BE3015AC-8269-4D94-AB27-E835CBFC3AFC}"
1515
EndProject
1616
Global
1717
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -35,10 +35,10 @@ Global
3535
{BD383672-7EF5-4737-91A9-92918CF2BBA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
3636
{BD383672-7EF5-4737-91A9-92918CF2BBA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
3737
{BD383672-7EF5-4737-91A9-92918CF2BBA7}.Release|Any CPU.Build.0 = Release|Any CPU
38-
{A614B4B7-AB24-421D-808F-C35D4139F8E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39-
{A614B4B7-AB24-421D-808F-C35D4139F8E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
40-
{A614B4B7-AB24-421D-808F-C35D4139F8E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
41-
{A614B4B7-AB24-421D-808F-C35D4139F8E6}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{BE3015AC-8269-4D94-AB27-E835CBFC3AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{BE3015AC-8269-4D94-AB27-E835CBFC3AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{BE3015AC-8269-4D94-AB27-E835CBFC3AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{BE3015AC-8269-4D94-AB27-E835CBFC3AFC}.Release|Any CPU.Build.0 = Release|Any CPU
4242
EndGlobalSection
4343
GlobalSection(SolutionProperties) = preSolution
4444
HideSolutionNode = FALSE
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsAspireSharedProject>true</IsAspireSharedProject>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
12+
13+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.1.0"/>
14+
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="10.1.0"/>
15+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.14.0"/>
16+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.14.0"/>
17+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.14.0"/>
18+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.14.0"/>
19+
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.14.0"/>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Diagnostics.HealthChecks;
5+
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.ServiceDiscovery;
7+
using OpenTelemetry;
8+
using OpenTelemetry.Metrics;
9+
using OpenTelemetry.Trace;
10+
11+
namespace Microsoft.Extensions.Hosting;
12+
13+
// Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
14+
// This project should be referenced by each service project in your solution.
15+
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
16+
public static class Extensions
17+
{
18+
private const string HealthEndpointPath = "/health";
19+
private const string AlivenessEndpointPath = "/alive";
20+
21+
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
22+
{
23+
builder.ConfigureOpenTelemetry();
24+
25+
builder.AddDefaultHealthChecks();
26+
27+
builder.Services.AddServiceDiscovery();
28+
29+
builder.Services.ConfigureHttpClientDefaults(http =>
30+
{
31+
// Turn on resilience by default
32+
http.AddStandardResilienceHandler();
33+
34+
// Turn on service discovery by default
35+
http.AddServiceDiscovery();
36+
});
37+
38+
// Uncomment the following to restrict the allowed schemes for service discovery.
39+
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
40+
// {
41+
// options.AllowedSchemes = ["https"];
42+
// });
43+
44+
return builder;
45+
}
46+
47+
public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
48+
{
49+
builder.Logging.AddOpenTelemetry(logging =>
50+
{
51+
logging.IncludeFormattedMessage = true;
52+
logging.IncludeScopes = true;
53+
});
54+
55+
builder.Services.AddOpenTelemetry()
56+
.WithMetrics(metrics =>
57+
{
58+
metrics.AddAspNetCoreInstrumentation()
59+
.AddHttpClientInstrumentation()
60+
.AddRuntimeInstrumentation()
61+
.AddMeter("Duende.IdentityServer", "Duende.IdentityServer.Expirimental", "IdentityServer");
62+
})
63+
.WithTracing(tracing =>
64+
{
65+
tracing.AddSource(builder.Environment.ApplicationName)
66+
.AddAspNetCoreInstrumentation(tracing =>
67+
// Exclude health check requests from tracing
68+
tracing.Filter = context =>
69+
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
70+
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
71+
)
72+
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
73+
//.AddGrpcClientInstrumentation()
74+
.AddHttpClientInstrumentation()
75+
.AddSource("Duende.IdentityServer");
76+
});
77+
78+
builder.AddOpenTelemetryExporters();
79+
80+
return builder;
81+
}
82+
83+
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
84+
{
85+
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
86+
87+
if (useOtlpExporter)
88+
{
89+
builder.Services.AddOpenTelemetry().UseOtlpExporter();
90+
}
91+
92+
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
93+
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
94+
//{
95+
// builder.Services.AddOpenTelemetry()
96+
// .UseAzureMonitor();
97+
//}
98+
99+
return builder;
100+
}
101+
102+
public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
103+
{
104+
builder.Services.AddHealthChecks()
105+
// Add a default liveness check to ensure app is responsive
106+
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
107+
108+
return builder;
109+
}
110+
111+
public static WebApplication MapDefaultEndpoints(this WebApplication app)
112+
{
113+
// Adding health checks endpoints to applications in non-development environments has security implications.
114+
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
115+
if (app.Environment.IsDevelopment())
116+
{
117+
// All health checks must pass for app to be considered ready to accept traffic after starting
118+
app.MapHealthChecks(HealthEndpointPath);
119+
120+
// Only health checks tagged with the "live" tag must pass for app to be considered alive
121+
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
122+
{
123+
Predicate = r => r.Tags.Contains("live")
124+
});
125+
}
126+
127+
return app;
128+
}
129+
}

IdentityServer/v8/SessionManagement/Api/Api.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
1111
</ItemGroup>
1212

13+
<ItemGroup>
14+
<ProjectReference Include="..\..\Aspire.ServiceDefaults\Aspire.ServiceDefaults.csproj" />
15+
</ItemGroup>
16+
1317
</Project>

IdentityServer/v8/SessionManagement/Api/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
.CreateLogger();
1414

1515
var builder = WebApplication.CreateBuilder(args);
16+
17+
builder.AddServiceDefaults();
1618
builder.Services.AddSerilog();
1719

1820
builder.Services.AddControllers();
@@ -30,6 +32,8 @@
3032

3133
var app = builder.Build();
3234

35+
app.MapDefaultEndpoints();
36+
3337
app.UseRouting();
3438
app.UseAuthentication();
3539
app.UseAuthorization();

0 commit comments

Comments
 (0)