{"record":{"id":"b5e3baac578de6cf","repo":"dotnetcore/CAP","slug":"we-detected-that-you-are-using-icappublisher-in-dbcontext-b5e3ba","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.UseSqlServer()","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\\.UseSqlServer\\(\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/DotNetCore.CAP.SqlServer/CAP.SqlServerOptions.cs","lineNumber":35,"sourceCode":"    /// </summary>\n    public string ConnectionString { get; set; } = default!;\n}\n\ninternal class ConfigureSqlServerOptions : IConfigureOptions<SqlServerOptions>\n{\n    private readonly IServiceScopeFactory _serviceScopeFactory;\n\n    public ConfigureSqlServerOptions(IServiceScopeFactory serviceScopeFactory)\n    {\n        _serviceScopeFactory = serviceScopeFactory;\n    }\n\n    public void Configure(SqlServerOptions options)\n    {\n        if (options.DbContextType == null) return;\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.UseSqlServer()\");\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}","sourceCodeStart":17,"sourceCodeEnd":45,"githubUrl":"https://github.com/dotnetcore/CAP/blob/e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb/src/DotNetCore.CAP.SqlServer/CAP.SqlServerOptions.cs#L17-L45","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.UseSqlServer()\" thrown in dotnetcore/CAP.","triggerScenarios":"Thrown at src/DotNetCore.CAP.SqlServer/CAP.SqlServerOptions.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 storage extension: services.AddCAP(x => x.UseSqlServer(Configuration.GetConnectionString(\"...\"))).","For transactional publishing inside DbContext, use the CAP storage extension on DbContext options (options.UseSqlServer().UseCAPTransaction(...)) instead of 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"}