Introduction
Asynchronous processing is not only about sending a message to a queue. A reliable system also needs a clear project structure, a repeatable way to run the database and message broker, centralized configuration, and a way to determine whether dependent services are actually ready.
In this lesson, we will prepare the foundation of the sample solution used throughout the course. The ASP.NET Core Web API will accept requests from clients, the Application layer will contain use cases, the Infrastructure layer will communicate with external dependencies such as SQL Server and RabbitMQ, and the Worker layer will host long-running background processing in later lessons. We will not build a complete producer or consumer yet. Instead, we will create a stable environment on which those components can be built.
Target architecture
We will divide the solution into four main projects:
- AsyncProcessing.Api: HTTP endpoints, application startup, configuration, and health endpoints.
- AsyncProcessing.Application: use cases, service contracts, DTOs, and business rules.
- AsyncProcessing.Infrastructure: Entity Framework Core, SQL Server, RabbitMQ connectivity, and external service adapters.
- AsyncProcessing.Worker: a long-running worker process that can run independently from the API. Later lessons may place consumers and Hangfire processing here or in another host.
The dependency direction matters. The API may reference Application and Infrastructure. The Worker may also reference Application and Infrastructure. Application should not directly depend on Infrastructure. This keeps business rules independent from a specific database or messaging library and makes them easier to test.
Project dependency overview
AsyncProcessing.Api -> Application, Infrastructure
AsyncProcessing.Worker -> Application, Infrastructure
AsyncProcessing.Infrastructure -> Application
AsyncProcessing.Application -> no application-layer dependencyCreate the solution
First, verify that the .NET 10 SDK is installed:
dotnet --versionCreate the solution and projects:
mkdir AsyncProcessing
cd AsyncProcessing
dotnet new sln -n AsyncProcessing
dotnet new webapi -n AsyncProcessing.Api -f net10.0
dotnet new classlib -n AsyncProcessing.Application -f net10.0
dotnet new classlib -n AsyncProcessing.Infrastructure -f net10.0
dotnet new worker -n AsyncProcessing.Worker -f net10.0
dotnet sln add AsyncProcessing.Api/AsyncProcessing.Api.csproj
dotnet sln add AsyncProcessing.Application/AsyncProcessing.Application.csproj
dotnet sln add AsyncProcessing.Infrastructure/AsyncProcessing.Infrastructure.csproj
dotnet sln add AsyncProcessing.Worker/AsyncProcessing.Worker.csprojAdd the project references:
dotnet add AsyncProcessing.Api reference AsyncProcessing.Application AsyncProcessing.Infrastructure
dotnet add AsyncProcessing.Infrastructure reference AsyncProcessing.Application
dotnet add AsyncProcessing.Worker reference AsyncProcessing.Application AsyncProcessing.InfrastructureAdd SQL Server and EF Core packages to the Infrastructure project. Keep package versions compatible with the .NET 10 and EF Core versions used by your solution:
dotnet add AsyncProcessing.Infrastructure package Microsoft.EntityFrameworkCore.SqlServer
dotnet add AsyncProcessing.Infrastructure package Microsoft.EntityFrameworkCore.Design
dotnet add AsyncProcessing.Infrastructure package Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCoreTo avoid SDK differences between team members, you can add a global.json file and pin the SDK version installed on your development machines.
Prepare services with Docker Compose
Instead of asking every developer to install SQL Server and RabbitMQ separately, we will describe both services in Docker Compose. Each service runs in its own container. When the application runs directly on the host machine, it connects through published localhost ports. If the application is later moved into Compose, it should use the service names sqlserver and rabbitmq as host names.
Create docker-compose.yml in the solution root:
services:
sqlserver:
image: mcr.microsoft.com/mssql/server:2022-latest
container_name: asyncprocessing-sqlserver
environment:
ACCEPT_EULA: "Y"
MSSQL_SA_PASSWORD: "YourStrong!Passw0rd"
ports:
- "1433:1433"
volumes:
- sqlserver-data:/var/opt/mssql
healthcheck:
test: ["CMD-SHELL", "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P \"$$MSSQL_SA_PASSWORD\" -Q \"SELECT 1\" -C -b"]
interval: 10s
timeout: 5s
retries: 10
start_period: 20s
rabbitmq:
image: rabbitmq:4-management
container_name: asyncprocessing-rabbitmq
environment:
RABBITMQ_DEFAULT_USER: appuser
RABBITMQ_DEFAULT_PASS: apppassword
ports:
- "5672:5672"
- "15672:15672"
volumes:
- rabbitmq-data:/var/lib/rabbitmq
healthcheck:
test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"]
interval: 10s
timeout: 5s
retries: 10
start_period: 15s
volumes:
sqlserver-data:
rabbitmq-data:The two named volumes make the services persistent. Even if the containers are removed, the database and RabbitMQ data remain available. Be careful with docker compose down -v; it also removes the volumes and therefore resets local development data.
Start the services
docker compose up -d
docker compose psOpen RabbitMQ's management interface at http://localhost:15672. Use appuser as the user name and apppassword as the password. The RabbitMQ management plugin is useful during development for observing queues, exchanges, connections, and message rates. Do not use default or easily guessed credentials in production.
To inspect container logs, run:
docker compose logs -f rabbitmq
docker compose logs -f sqlserverConfiguration and options
We will keep connection strings out of application code and use appsettings.json together with environment-specific configuration. An example configuration for an API running on the local machine is:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost,1433;Database=AsyncProcessingDb;User Id=sa;Password=YourStrong!Passw0rd;TrustServerCertificate=True",
"RabbitMq": "amqp://appuser:apppassword@localhost:5672/"
},
"RabbitMq": {
"Host": "localhost",
"Port": 5672,
"UserName": "appuser",
"Password": "apppassword",
"VirtualHost": "/"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}Do not commit passwords to source control. Use appsettings.Development.json, user secrets, or environment variables for local secrets. For example:
dotnet user-secrets init --project AsyncProcessing.Api
dotnet user-secrets set "ConnectionStrings:DefaultConnection" "Server=localhost,1433;Database=AsyncProcessingDb;User Id=sa;Password=YourStrong!Passw0rd;TrustServerCertificate=True" --project AsyncProcessing.ApiUse a typed options class for RabbitMQ settings:
namespace AsyncProcessing.Infrastructure.Configuration;
public sealed class RabbitMqOptions
{
public const string SectionName = "RabbitMq";
public string Host { get; init; } = "localhost";
public int Port { get; init; } = 5672;
public string UserName { get; init; } = string.Empty;
public string Password { get; init; } = string.Empty;
public string VirtualHost { get; init; } = "/";
}Dependency Injection and DbContext
Create a simple DbContext in Infrastructure:
using Microsoft.EntityFrameworkCore;
namespace AsyncProcessing.Infrastructure.Persistence;
public sealed class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
}Add an Infrastructure registration method:
using AsyncProcessing.Infrastructure.Configuration;
using AsyncProcessing.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace AsyncProcessing.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(
this IServiceCollection services,
IConfiguration configuration)
{
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(
configuration.GetConnectionString("DefaultConnection")));
services.AddOptions<RabbitMqOptions>()
.Bind(configuration.GetSection(RabbitMqOptions.SectionName))
.Validate(options => !string.IsNullOrWhiteSpace(options.Host),
"RabbitMQ host is required")
.ValidateOnStart();
return services;
}
}Use the registration method in the API's Program.cs:
using AsyncProcessing.Infrastructure;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddInfrastructure(builder.Configuration);
builder.Services.AddHealthChecks();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
app.MapHealthChecks("/health");
app.Run();AddHealthChecks and MapHealthChecks provide a basic availability endpoint. You can also register a DbContext health check:
builder.Services
.AddHealthChecks()
.AddDbContextCheck<AppDbContext>("sqlserver");This verifies not only that the API process is running, but also that it can connect to SQL Server. Do not expose connection strings or sensitive dependency details in the health response.
Worker project entry point
The Worker project can initially be used to confirm that the hosting environment is working:
using AsyncProcessing.Infrastructure;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddInfrastructure(builder.Configuration);
builder.Services.AddHostedService<Worker>();
var host = builder.Build();
host.Run();The Worker also needs the same connection settings in its appsettings.json. In a real application, use shared configuration conventions or environment variables so that API and Worker settings remain consistent. When a scoped service is used inside a Worker, an appropriate scope must be created for each unit of work; we will cover this in later lessons.
Run and verify
- Start both services with
docker compose up -d. - Use
docker compose psto verify that the containers are running and their health status is ready. - Start the API with
dotnet run --project AsyncProcessing.Api. - Call
/healthand verify theHealthyresponse. - Sign in to the RabbitMQ management interface with
appuser. - Verify the SQL Server connection with an EF Core migration or a simple connectivity test.
Common mistakes
- Confusing localhost: When the API runs on the host machine, SQL Server is reached through
localhost,1433. When the API runs in a container, usesqlserver,1433. - Assuming started means ready: A running container does not necessarily accept connections yet. Health checks and retry policies are still necessary.
- Using a weak SA password: SQL Server requires a strong password. Never place it in public source files.
- Deleting volumes accidentally:
down -vremoves local development data. Use it only when a clean reset is intentional. - Using inconsistent API and Worker settings: Check whether each process runs on the host or inside Compose before choosing
localhostorrabbitmq. - Treating health checks as business logic: A health endpoint is for observability. It is not a replacement for authentication, authorization, or domain validation.
Short practice exercise
Add a Demo API endpoint to the solution. It should return 200 OK with a small JSON response indicating that the application is running. Then call /health and /demo and answer these questions:
- What happens to
/healthwhen the RabbitMQ container is stopped? - How does the DbContext health check behave when SQL Server is stopped?
- Why must the RabbitMQ host change to
rabbitmqwhen the API is moved into Compose? - Are database records preserved when the SQL Server container is recreated without deleting volumes?
Conclusion
In this lesson, we built the starting architecture for a .NET 10 asynchronous processing solution. We separated the responsibilities of the API, Application, Infrastructure, and Worker projects; ran SQL Server and RabbitMQ with Docker Compose; configured typed options and dependency injection; and prepared a basic health endpoint.
In the next lessons, we will build RabbitMQ connections, exchanges, queues, producers, and consumers on top of this foundation. Setting up the environment correctly from the beginning reduces connection errors, incorrect service addresses, and deployment surprises.
Sources
- https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-10.0
- https://learn.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-10.0
- https://docs.docker.com/reference/compose-file/services/
- https://docs.docker.com/compose/how-tos/startup-order/
- https://www.rabbitmq.com/docs/management