Quickstart
Build an API that returns the current tenant, rejects unknown tenants, and exposes a health endpoint that works without a tenant. You need a terminal and the latest stable .NET SDK; see Compatibility.
1. Create the application
sh
dotnet new web --name TenantApi
cd TenantApi
dotnet add package Ayaka.MultiTenancy --prerelease
dotnet add package Ayaka.MultiTenancy.AspNetCore --prerelease2. Register services and endpoints
Replace Program.cs with:
csharp
using Ayaka.MultiTenancy;
using Ayaka.MultiTenancy.AspNetCore;
using Ayaka.MultiTenancy.DependencyInjection;
using Ayaka.MultiTenancy.Management;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddProblemDetails();
builder.Services.AddMultiTenancy()
.ConfigureRequestTenancy(request => request
.DetectFromRequestHeader("X-Tenant")
.ResolveWithTenantManagement())
.AddTenantManagement()
.UseInMemoryStore();
var app = builder.Build();
var tenants = app.Services.GetRequiredService<ITenantManager>();
await tenants.AddAsync(new Tenant("acme", "Acme"));
await tenants.AddAsync(new Tenant("contoso", "Contoso"));
app.UseRouting();
app.UseMultiTenancy();
app.MapGet("/tenant", (ITenantContextAccessor accessor) => Results.Ok(accessor.TenantContext));
app.MapGet("/health", () => Results.Ok("Healthy"))
.WithMetadata(new DisableMultiTenancyAttribute());
await app.RunAsync();What each part does:
AddMultiTenancy()registers the tenant context accessor.ConfigureRequestTenancy(...)detects the tenant from theX-Tenantheader and resolves it through tenant management, so unknown tenants are rejected.AddTenantManagement().UseInMemoryStore()adds the tenant manager with a store that lives in the running process. The application seeds it before accepting requests.UseRouting()selects an endpoint beforeUseMultiTenancy()checks its metadata. The middleware then makes the tenant available throughITenantContextAccessor.
3. Run it
sh
dotnet run --no-launch-profile --urls http://localhost:5080In another terminal, send a request:
sh
curl -i -H "X-Tenant: acme" http://localhost:5080/tenantpowershell
Invoke-WebRequest http://localhost:5080/tenant -Headers @{ 'X-Tenant' = 'acme' } -SkipHttpErrorCheckYou should receive 200 OK with:
json
{ "id": "acme", "displayName": "Acme" }Change the header to X-Tenant: contoso to get Contoso's context. Try these cases as well:
| Request | Expected result |
|---|---|
/tenant without X-Tenant | 400, problem title Tenant could not be detected. |
/tenant with X-Tenant: unknown | 404, problem title Tenant is unknown. |
/health without X-Tenant | 200, because the endpoint disables multi-tenancy. |
The problem responses come from AddProblemDetails(). Without a problem-details service, the middleware still sets the status code but does not write a response body.
Next steps
This example loses its tenants when the process restarts, and it selects tenants without authenticating callers. To go further:
- Database-backed tenant store — persist tenants and cache lookups.
- Tenant context — how the current tenant flows through your code.
- Request pipeline — where to place the middleware in a real application.