Skip to content

Commit 901ec47

Browse files
authored
feat: Add .NET Aspire (#2827)
* feat: Add .NET Aspire * fix: Fix the Connection String * fix: Update markdown and published * fix: publish the API only
1 parent 7c23544 commit 901ec47

File tree

21 files changed

+427
-31
lines changed

21 files changed

+427
-31
lines changed

.github/workflows/build-and-dockerize.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ jobs:
3939
uses: actions/setup-dotnet@v4
4040
with:
4141
dotnet-version: '9.0.x'
42-
dotnet-quality: 'preview'
4342
- name: Install dependencies
4443
run: dotnet restore
4544
- name: Build
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Sdk Name="Aspire.AppHost.Sdk" Version="9.1.0" />
3+
4+
<PropertyGroup>
5+
<OutputType>Exe</OutputType>
6+
<TargetFramework>net9.0</TargetFramework>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
<IsAspireHost>true</IsAspireHost>
10+
<UserSecretsId>0f1966e4-7b2b-4a28-a9b6-8198aab433ec</UserSecretsId>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.1.0" />
15+
<PackageReference Include="Aspire.Hosting.Azure.ServiceBus" Version="9.1.0" />
16+
<PackageReference Include="Aspire.Hosting.Azure.Storage" Version="9.1.0" />
17+
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.1.0" />
18+
<PackageReference Include="Aspire.Hosting.Redis" Version="9.1.0" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\BervProject.WebApi.Boilerplate\BervProject.WebApi.Boilerplate.csproj" />
23+
<ProjectReference Include="..\BervProject.WebApi.Boilerplate.MigrationService\BervProject.WebApi.Boilerplate.MigrationService.csproj" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var builder = DistributedApplication.CreateBuilder(args);
2+
3+
var cache = builder.AddRedis("cache").WithRedisInsight();
4+
var postgres = builder.AddPostgres("postgres").WithPgAdmin();
5+
var postgresdb = postgres.AddDatabase("postgresdb");
6+
var serviceBus = builder.AddAzureServiceBus("messaging").RunAsEmulator();
7+
var storage = builder.AddAzureStorage("storage").RunAsEmulator();
8+
var blobs = storage.AddBlobs("blobs");
9+
var queues = storage.AddQueues("queues");
10+
var tables = storage.AddTables("tables");
11+
12+
var migration = builder.AddProject<Projects.BervProject_WebApi_Boilerplate_MigrationService>("migrations")
13+
.WithReference(postgresdb, connectionName: "BoilerplateConnectionString")
14+
.WithExplicitStart();
15+
16+
builder.AddProject<Projects.BervProject_WebApi_Boilerplate>("apiservice")
17+
.WithHttpEndpoint()
18+
.WithReference(cache, connectionName: "Redis")
19+
.WithReference(postgresdb, connectionName: "BoilerplateConnectionString")
20+
.WithReference(blobs, connectionName: "AzureStorageBlob")
21+
.WithReference(queues, connectionName: "AzureStorageQueue")
22+
.WithReference(tables, connectionName: "AzureStorageTable")
23+
.WithReference(serviceBus, connectionName: "AzureServiceBus")
24+
.WaitFor(cache)
25+
.WaitFor(postgresdb)
26+
.WaitFor(blobs)
27+
.WaitFor(queues)
28+
.WaitFor(tables)
29+
.WaitFor(serviceBus)
30+
.WaitForCompletion(migration);
31+
32+
builder.Build().Run();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning",
6+
"Aspire.Hosting.Dcp": "Warning"
7+
}
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Worker">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<UserSecretsId>dotnet-BervProject.WebApi.Boilerplate.MigrationService-02d1add9-9cd8-4b0e-96ab-9dedc82c871c</UserSecretsId>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.1.0" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.3" />
13+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.3" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\BervProject.WebApi.Boilerplate\BervProject.WebApi.Boilerplate.csproj" />
18+
<ProjectReference Include="..\BervProject.WebApi.Boilerplate.ServiceDefaults\BervProject.WebApi.Boilerplate.ServiceDefaults.csproj" />
19+
</ItemGroup>
20+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using BervProject.WebApi.Boilerplate.EntityFramework;
2+
using BervProject.WebApi.Boilerplate.MigrationService;
3+
4+
var builder = Host.CreateApplicationBuilder(args);
5+
6+
builder.AddServiceDefaults();
7+
8+
builder.Services.AddHostedService<Worker>();
9+
10+
builder.Services.AddOpenTelemetry()
11+
.WithTracing(tracing => tracing.AddSource(Worker.ActivitySourceName));
12+
builder.AddNpgsqlDbContext<BoilerplateDbContext>("BoilerplateConnectionString");
13+
14+
var host = builder.Build();
15+
host.Run();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Diagnostics;
2+
using BervProject.WebApi.Boilerplate.EntityFramework;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace BervProject.WebApi.Boilerplate.MigrationService;
6+
7+
public class Worker : BackgroundService
8+
{
9+
public const string ActivitySourceName = "Migrations";
10+
private static readonly ActivitySource SActivitySource = new(ActivitySourceName);
11+
12+
private readonly IServiceProvider _serviceProvider;
13+
private readonly IHostApplicationLifetime _hostApplicationLifetime;
14+
15+
public Worker(IServiceProvider serviceProvider,
16+
IHostApplicationLifetime hostApplicationLifetime)
17+
{
18+
_serviceProvider = serviceProvider;
19+
_hostApplicationLifetime = hostApplicationLifetime;
20+
}
21+
22+
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
23+
{
24+
using var activity = SActivitySource.StartActivity("Migrating database", ActivityKind.Client);
25+
26+
try
27+
{
28+
using var scope = _serviceProvider.CreateScope();
29+
var dbContext = scope.ServiceProvider.GetRequiredService<BoilerplateDbContext>();
30+
31+
await RunMigrationAsync(dbContext, cancellationToken);
32+
}
33+
catch (Exception ex)
34+
{
35+
activity?.AddException(ex);
36+
throw;
37+
}
38+
39+
_hostApplicationLifetime.StopApplication();
40+
}
41+
42+
private static async Task RunMigrationAsync(BoilerplateDbContext dbContext, CancellationToken cancellationToken)
43+
{
44+
var strategy = dbContext.Database.CreateExecutionStrategy();
45+
await strategy.ExecuteAsync(async () =>
46+
{
47+
// Run migration in a transaction to avoid partial migration if it fails.
48+
await dbContext.Database.MigrateAsync(cancellationToken);
49+
});
50+
}
51+
52+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.Hosting.Lifetime": "Information"
6+
}
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.Hosting.Lifetime": "Information"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)