Skip to content

Hosting Extensions

The CritterWatch.Services package exposes two extension methods for ASP.NET Core applications.

AddCritterWatch()

Registers all CritterWatch dependencies into the ASP.NET Core IServiceCollection / IHostApplicationBuilder:

csharp
builder.AddCritterWatch(
    connectionString: "Host=localhost;Database=critterwatch;...",
    configureWolverine: opts =>
    {
        opts.UseRabbitMq(new Uri("amqp://localhost")).AutoProvision();
        opts.ListenToRabbitQueue("critterwatch").Sequential();
    });

What It Registers

Wolverine:

  • Configures Wolverine with all CritterWatch message handlers
  • Sets up message routing for commands dispatched to monitored services
  • Applies the provided configureWolverine action for transport setup

Marten:

  • Registers Marten with PostgreSQL using the provided connection string
  • Schema: critterwatch
  • Projections: ServiceSummaryProjection (single stream), TimelineProjection (cross-stream)
  • Async projections run in-process via Marten's AddAsyncDaemon()

SignalR:

  • Registers the CommunicationHub
  • Configures JSON serialization with camelCase and string enum converters

Alert System:

  • Registers alert evaluator services
  • Applies default threshold configuration

Other:

  • Registers background services for periodic maintenance tasks
  • Configures health check services for the Marten database

Overloads

csharp
// Connection string + Wolverine config
builder.AddCritterWatch(connectionString, configureWolverine);

// Connection string + Wolverine config + alert thresholds
builder.AddCritterWatch(connectionString, configureWolverine, configureAlerts);

// Full options object
builder.AddCritterWatch(options => {
    options.ConnectionString = "...";
    options.SignalRRoute = "/api/messages";
    options.ConfigureWolverine = opts => { ... };
    options.ConfigureAlerts = alerts => { ... };
});

UseCritterWatch()

Maps CritterWatch middleware into the ASP.NET Core request pipeline:

csharp
app.UseCritterWatch();

// With custom SignalR route:
app.UseCritterWatch(signalRRoute: "/my-hub");

What It Maps

Wolverine HTTP:

  • Maps all HTTP endpoints defined with [WolverineGet], [WolverinePost], etc. in CritterWatch.Services
  • These are mounted under /api/critterwatch/*

SignalR Hub:

csharp
app.MapHub<CommunicationHub>("/api/messages");

Static Files (Embedded SPA):

  • Configures StaticFileMiddleware to serve embedded Vue SPA resources
  • Falls back to index.html for all unmatched routes (client-side routing)

Call Order

UseCritterWatch() should be called after UseRouting() and any authentication/authorization middleware, but before the fallback handler. The typical order is:

csharp
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseCritterWatch();
app.Run();

Marten Configuration

CritterWatch creates a dedicated Marten DocumentStore for its internal state. This is independent of any Marten stores your application may configure — they do not conflict.

The store is configured with:

  • Schema: critterwatch (all tables, functions, and sequences live in this schema)
  • Document types: ServiceSummary, AgentHealthState, AlertRecord, TimelineEntry
  • Event types: all 25+ domain event types from Events.cs
  • Projections: ServiceSummaryProjection, TimelineProjection
  • Async daemon: started automatically for continuous projection updates

Connection String Security

The PostgreSQL connection string is stored only in your application configuration and is never transmitted to monitored services or the browser. Use appsettings.json with environment variable overrides for production deployments.

Released under the MIT License.