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.UseMySql()

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.UseMySql()" thrown in dotnetcore/CAP.

Solutions

  1. Remove ICapPublisher usage from your DbContext and configure CAP storage with the dedicated extension instead: services.AddCAP(x => x.UseMySql(Configuration.GetConnectionString("..."))).
  2. If you need transactions inside DbContext, inject ICapTransaction or use the CAP storage extension on the DbContext options (options.UseMySql().UseCAPTransaction(...)) rather than injecting the publisher.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/DotNetCore.CAP.MySql/CAP.MySqlOptions.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/2c7ba1e38ec13d8c. Report an issue: GitHub.

Appendix: source

Thrown at src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs:35

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

internal class ConfigureMySqlOptions : IConfigureOptions<MySqlOptions>
{
    private readonly IServiceScopeFactory _serviceScopeFactory;

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

    public void Configure(MySqlOptions options)
    {
        if (options.DbContextType != null)
        {
            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.UseMySql()");

            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)