Skip to content

Commit

Permalink
- Aspire is added.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadyNagy committed Dec 8, 2024
1 parent ad14262 commit 41bca8d
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 4 deletions.
36 changes: 33 additions & 3 deletions DevBetterWeb.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{C18577A9-A101-4654-89A2-CEC4D57B4D0C}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.env = .env
Directory.Packages.props = Directory.Packages.props
docker-compose.yml = docker-compose.yml
Dockerfile = Dockerfile
efscripts.txt = efscripts.txt
global.json = global.json
README.md = README.md
Dockerfile = Dockerfile
docker-compose.yml = docker-compose.yml
.env = .env
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevBetterWeb.UnitTests", "tests\DevBetterWeb.UnitTests\DevBetterWeb.UnitTests.csproj", "{0AB375BC-E7AD-4BD9-8D28-AC07351CE37A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevBetterWeb.FunctionalTests", "tests\DevBetterWeb.FunctionalTests\DevBetterWeb.FunctionalTests.csproj", "{26C52F3F-8053-4891-9D9D-E8BB99D566A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevBetterWeb.AspireServiceDefaults", "src\DevBetterWeb.AspireServiceDefaults\DevBetterWeb.AspireServiceDefaults.csproj", "{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevBetterWeb.AppHost", "src\DevBetterWeb.AppHost\DevBetterWeb.AppHost.csproj", "{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -118,6 +122,30 @@ Global
{26C52F3F-8053-4891-9D9D-E8BB99D566A5}.Release|x64.Build.0 = Release|Any CPU
{26C52F3F-8053-4891-9D9D-E8BB99D566A5}.Release|x86.ActiveCfg = Release|Any CPU
{26C52F3F-8053-4891-9D9D-E8BB99D566A5}.Release|x86.Build.0 = Release|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Debug|x64.ActiveCfg = Debug|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Debug|x64.Build.0 = Debug|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Debug|x86.ActiveCfg = Debug|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Debug|x86.Build.0 = Debug|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Release|Any CPU.Build.0 = Release|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Release|x64.ActiveCfg = Release|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Release|x64.Build.0 = Release|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Release|x86.ActiveCfg = Release|Any CPU
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2}.Release|x86.Build.0 = Release|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Debug|x64.ActiveCfg = Debug|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Debug|x64.Build.0 = Debug|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Debug|x86.ActiveCfg = Debug|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Debug|x86.Build.0 = Debug|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Release|Any CPU.Build.0 = Release|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Release|x64.ActiveCfg = Release|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Release|x64.Build.0 = Release|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Release|x86.ActiveCfg = Release|Any CPU
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -129,6 +157,8 @@ Global
{AEE17BAB-D187-4BC5-B640-40CC0749566C} = {B31B4797-1D9F-4288-808C-BE9A31A98C7D}
{0AB375BC-E7AD-4BD9-8D28-AC07351CE37A} = {B31B4797-1D9F-4288-808C-BE9A31A98C7D}
{26C52F3F-8053-4891-9D9D-E8BB99D566A5} = {B31B4797-1D9F-4288-808C-BE9A31A98C7D}
{2883209B-BC68-4D3B-9E18-8F49F4EECCD2} = {106AE906-5075-410A-B941-912F811848EE}
{DEBA39F9-2AA7-4009-B4F3-61C51B999F8C} = {106AE906-5075-410A-B941-912F811848EE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B0F19343-8185-4A9F-9165-0EA8544BC925}
Expand Down
13 changes: 12 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" PrivateAssets="all" Version="8.0.4" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Identity.Core" Version="8.0.4" />
<PackageVersion Include="Microsoft.Extensions.Identity.Stores" Version="8.0.4" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
Expand Down Expand Up @@ -81,6 +81,17 @@
<PackageVersion Include="TagHelperSamples.Authorization" Version="3.0.56" />
<PackageVersion Include="Westwind.AspNetCore.Markdown" Version="3.12.0" />
<PackageVersion Include="WindowsAzure.Storage" Version="9.3.3" />
<!-- Aspire -->
<PackageVersion Include="Aspire.Hosting.AppHost" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http.Resilience" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.ServiceDiscovery" Version="8.0.0" />
<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="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-beta.2" />
<!-- Test -->
<PackageVersion Include="xunit" Version="2.6.6" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageVersion Include="xunit.runner.console" Version="2.9.0">
Expand Down
22 changes: 22 additions & 0 deletions src/DevBetterWeb.AppHost/DevBetterWeb.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>96f9945c-8c26-4166-986f-ba0b96189d64</UserSecretsId>
</PropertyGroup>

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

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

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

builder
.AddProject<Projects.DevBetterWeb_Web>(nameof(Projects.DevBetterWeb_Web).Replace("_", string.Empty).ToLower());

builder.Build().Run();
29 changes: 29 additions & 0 deletions src/DevBetterWeb.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:17241;http://localhost:15125",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21066",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22203"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15125",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19248",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20035"
}
}
}
}
8 changes: 8 additions & 0 deletions src/DevBetterWeb.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/DevBetterWeb.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"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</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.Exporter.Prometheus.AspNetCore" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
</ItemGroup>

</Project>
129 changes: 129 additions & 0 deletions src/DevBetterWeb.AspireServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
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 IHostApplicationBuilder AddAspireServiceDefaults(this IHostApplicationBuilder builder)
{
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 IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation()
.AddMeter(
"Microsoft.AspNetCore.Hosting",
"Microsoft.AspNetCore.Server.Kestrel",
"System.Net.Http"
);
})
.WithTracing(tracing =>
{
if (builder.Environment.IsDevelopment())
{
tracing.SetSampler<AlwaysOnSampler>();
}
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 IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}

builder.Services.AddOpenTelemetry().WithMetrics(x => x.AddPrometheusExporter());
// 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 IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
{
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)
{
app.MapPrometheusScrapingEndpoint();

// 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;
}
}
1 change: 1 addition & 0 deletions src/DevBetterWeb.Web/DevBetterWeb.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<PackageReference Include="Flurl.Http" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DevBetterWeb.AspireServiceDefaults\DevBetterWeb.AspireServiceDefaults.csproj" />
<ProjectReference Include="..\DevBetterWeb.Core\DevBetterWeb.Core.csproj" />
<ProjectReference Include="..\DevBetterWeb.Infrastructure\DevBetterWeb.Infrastructure.csproj" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/DevBetterWeb.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

var builder = WebApplication.CreateBuilder(args);

builder.AddAspireServiceDefaults();

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());

builder.Host.UseSerilog((_, config) => config.ReadFrom.Configuration(builder.Configuration));
Expand Down

0 comments on commit 41bca8d

Please sign in to comment.