website/src/docs/mocha/v16/hosting.md
The Mocha.Hosting package provides ASP.NET Core integrations for the message bus: health checks that verify end-to-end connectivity through serialization, transport, routing, and handler execution.
dotnet add package Mocha.Hosting
Mocha integrates with the ASP.NET Core health checks system. The health check sends a HealthRequest message through the bus and waits for a HealthResponse. This verifies the full pipeline - serialization, transport, routing, and handler execution - not just that the broker is reachable.
On the bus builder, call .AddHealthCheck() to register the built-in HealthRequestHandler. This handler responds to HealthRequest messages with an "OK" response.
using Mocha;
using Mocha.Hosting;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddMessageBus()
.AddHealthCheck() // Registers the HealthRequestHandler
.AddMyApp() // source-generated handler registration
.AddRabbitMQ();
Use the AddMessageBus() extension on IHealthChecksBuilder to register a health check that sends a request through the bus and verifies the response:
builder.Services
.AddHealthChecks()
.AddMessageBus(); // Sends HealthRequest via RequestAsync and checks the reply
Then map the health check endpoint:
var app = builder.Build();
app.MapHealthChecks("/health");
app.Run();
A GET /health request will now include the message bus status. If the bus cannot process and reply to the health request within the timeout, the check reports Unhealthy.
By default the health check uses the bus's default routing to deliver the HealthRequest. To target a specific endpoint (useful when you have multiple transports or want to verify a particular service), pass a URI:
builder.Services
.AddHealthChecks()
.AddMessageBus(new Uri("queue://my-service-health"));
The health check is registered with the "ready" and "live" tags, so you can use tag-based filtering to separate readiness from liveness probes:
app.MapHealthChecks("/health/ready", new()
{
Predicate = check => check.Tags.Contains("ready")
});
app.MapHealthChecks("/health/live", new()
{
Predicate = check => check.Tags.Contains("live")
});