dotnetcore/CAP · error · InvalidOperationException

AddCap() must be added on the service collection. eg…

Error message

AddCap() must be added on the service collection.   eg: services.AddCap(...)

What it means

A bootstrap-time configuration guard in CheckRequirement: the CapMarkerService is not present in DI, which proves services.AddCAP(...) was never called before the CAP bootstrapper started. It fires during application startup (from BootstrapAsync), not during message processing, and simply means CAP was never registered on the service collection.

Solutions

  1. Call services.AddCAP(options => ...) on your IServiceCollection during startup (Program.cs or Startup.ConfigureServices).
  2. If using a partitioned/layered registration, make sure the AddCAP call executes in the same service collection used to build the final IServiceProvider.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/DotNetCore.CAP/Internal/IBootstrapper.Default.cs:131 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of dotnetcore/CAP@e52b8508e5 (2026-09-14). Data as JSON: /api/errors/133f5fec64601d18. Report an issue: GitHub.

Appendix: source

Thrown at src/DotNetCore.CAP/Internal/IBootstrapper.Default.cs:131

    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await BootstrapAsync(stoppingToken).ConfigureAwait(false);
    }

    public override async Task StopAsync(CancellationToken cancellationToken)
    {
        _cts?.Cancel();

        await base.StopAsync(cancellationToken).ConfigureAwait(false);
    }

    private void CheckRequirement()
    {
        var marker = _serviceProvider.GetService<CapMarkerService>();
        if (marker == null)
            throw new InvalidOperationException(
                "AddCap() must be added on the service collection.   eg: services.AddCap(...)");

        var messageQueueMarker = _serviceProvider.GetService<CapMessageQueueMakerService>();
        if (messageQueueMarker == null)
            throw new InvalidOperationException(
                "You must be config transport provider for CAP!" + Environment.NewLine +
                "==================================================================================" +
                Environment.NewLine +
                "========   eg: services.AddCap( options => { options.UseRabbitMQ(...) }); ========" +
                Environment.NewLine +
                "==================================================================================");

        var databaseMarker = _serviceProvider.GetService<CapStorageMarkerService>();
        if (databaseMarker == null)
            throw new InvalidOperationException(
                "You must be config storage provider for CAP!" + Environment.NewLine +
                "===================================================================================" +
                Environment.NewLine +

View on GitHub (pinned to e52b8508e5)