dotnetcore/CAP · error · InvalidOperationException

We detected that you are using ICapPublisher in DbContext…

Error message

We detected that you are using ICapPublisher in DbContext, please change the configuration to use the storage extension directly to avoid circular references! eg:  x.UseSqlServer()

What it means

Error "We detected that you are using ICapPublisher in DbContext, please change the configuration to use the storage extension directly to avoid circular references! eg: x.UseSqlServer()" thrown in dotnetcore/CAP.

Solutions

  1. Remove ICapPublisher usage from your DbContext and configure CAP storage with the storage extension: services.AddCAP(x => x.UseSqlServer(Configuration.GetConnectionString("..."))).
  2. For transactional publishing inside DbContext, use the CAP storage extension on DbContext options (options.UseSqlServer().UseCAPTransaction(...)) instead of injecting the publisher.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/DotNetCore.CAP.SqlServer/CAP.SqlServerOptions.cs:35 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/b5e3baac578de6cf. Report an issue: GitHub.

Appendix: source

Thrown at src/DotNetCore.CAP.SqlServer/CAP.SqlServerOptions.cs:35

    /// </summary>
    public string ConnectionString { get; set; } = default!;
}

internal class ConfigureSqlServerOptions : IConfigureOptions<SqlServerOptions>
{
    private readonly IServiceScopeFactory _serviceScopeFactory;

    public ConfigureSqlServerOptions(IServiceScopeFactory serviceScopeFactory)
    {
        _serviceScopeFactory = serviceScopeFactory;
    }

    public void Configure(SqlServerOptions options)
    {
        if (options.DbContextType == null) return;

        if (Helper.IsUsingType<ICapPublisher>(options.DbContextType))
            throw new InvalidOperationException(
                "We detected that you are using ICapPublisher in DbContext, please change the configuration to use the storage extension directly to avoid circular references! eg:  x.UseSqlServer()");

        using var scope = _serviceScopeFactory.CreateScope();
        var provider = scope.ServiceProvider;
        using var dbContext = (DbContext)provider.GetRequiredService(options.DbContextType);
        var connectionString = dbContext.Database.GetConnectionString();
        if (string.IsNullOrEmpty(connectionString)) throw new ArgumentNullException(connectionString);
        options.ConnectionString = connectionString;
    }
}

View on GitHub (pinned to e52b8508e5)