The Aspire ServiceDefaults project template provided by the SDK requires the ASP.NET Core shared runtime to be installed in production for all your applications. This is problematic for solutions that include other application types like Worker applications.
Specifically, Worker applications (IHostedService
) typically only require the dotnet
shared runtime.
// Does NOT require the aspnetcore shared runtime
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<Worker>();
If you run this in Docker, you would use the dotnet
base image, which doesn't include the aspnetcore
runtime.
FROM mcr.microsoft.com/dotnet/runtime:8.0
Adding the service defaults introduces a dependency on Microsoft.AspNetCore.App
, which requires the aspnetcore
shared runtime.
// DOES require the aspnetcore shared runtime
var builder = Host.CreateApplicationBuilder(args);
builder.AddServiceDefaults();
builder.Services.AddHostedService<Worker>();
So, the docker base image would have to be changed.
-FROM mcr.microsoft.com/dotnet/runtime:8.0
+FROM mcr.microsoft.com/dotnet/aspnet:8.0
Instead, you can split the ServiceDefaults
project into DotNetDefaults
which only requires the dotnet
runtime, and AspNetDefaults
for code that requires the aspnetcore
runtime. The AspNetDefaults
extend the DotNetDefaults
for maximal code reuse.
The updated worker application:
var builder = Host.CreateApplicationBuilder(args);
// Add project reference to DotNetDefaults.csproj
// Included in this Gist
builder.AddDotNetDefaults();
var host = builder.Build();
host.Run();
Web applications:
var builder = WebApplication.CreateBuilder(args);
// Add project reference to AspNetDefaults.csproj
// Included in this Gist
builder.AddAspNetDefaults();
var app = builder.Build();
app.MapDefaultEndpoints();
app.Run();