{"record":{"id":"2c7ba1e38ec13d8c","repo":"dotnetcore/CAP","slug":"we-detected-that-you-are-using-icappublisher-in-dbcontext","errorCode":null,"errorMessage":"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()","messagePattern":"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\\(\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs","lineNumber":35,"sourceCode":"    /// </summary>\n    public string ConnectionString { get; set; } = default!;\n}\n\ninternal class ConfigureMySqlOptions : IConfigureOptions<MySqlOptions>\n{\n    private readonly IServiceScopeFactory _serviceScopeFactory;\n\n    public ConfigureMySqlOptions(IServiceScopeFactory serviceScopeFactory)\n    {\n        _serviceScopeFactory = serviceScopeFactory;\n    }\n\n    public void Configure(MySqlOptions options)\n    {\n        if (options.DbContextType != null)\n        {\n            if (Helper.IsUsingType<ICapPublisher>(options.DbContextType))\n                throw new InvalidOperationException(\n                    \"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()\");\n\n            using var scope = _serviceScopeFactory.CreateScope();\n            var provider = scope.ServiceProvider;\n            using var dbContext = (DbContext)provider.GetRequiredService(options.DbContextType);\n            var connectionString = dbContext.Database.GetConnectionString();\n            if (string.IsNullOrEmpty(connectionString)) throw new ArgumentNullException(connectionString);\n            options.ConnectionString = connectionString;\n        }\n    }\n}","sourceCodeStart":17,"sourceCodeEnd":46,"githubUrl":"https://github.com/dotnetcore/CAP/blob/e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb/src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs#L17-L46","documentation":"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.","triggerScenarios":"Thrown at src/DotNetCore.CAP.MySql/CAP.MySqlOptions.cs:35 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Remove ICapPublisher usage from your DbContext and configure CAP storage with the dedicated extension instead: services.AddCAP(x => x.UseMySql(Configuration.GetConnectionString(\"...\"))).","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb","analyzedAt":"2026-09-14T14:46:34.677Z","contentChangedAt":"2026-09-14T14:46:34.677Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}