{"record":{"id":"0413a87e69ab46ff","repo":"dotnetcore/CAP","slug":"we-detected-that-you-are-using-icappublisher-in-dbcontext-0413a8","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.UsePostgreSql()","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\\.UsePostgreSql\\(\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlOptions.cs","lineNumber":51,"sourceCode":"        return DataSource != null ? DataSource.CreateConnection() : new NpgsqlConnection(ConnectionString);\n    }\n}\n\ninternal class ConfigurePostgreSqlOptions : IConfigureOptions<PostgreSqlOptions>\n{\n    private readonly IServiceScopeFactory _serviceScopeFactory;\n\n    public ConfigurePostgreSqlOptions(IServiceScopeFactory serviceScopeFactory)\n    {\n        _serviceScopeFactory = serviceScopeFactory;\n    }\n\n    public void Configure(PostgreSqlOptions 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.UsePostgreSql()\");\n\n        using var scope = _serviceScopeFactory.CreateScope();\n        var provider = scope.ServiceProvider;\n        using var dbContext = (DbContext)provider.GetRequiredService(options.DbContextType);\n\n        var coreOptions = dbContext.GetService<IDbContextOptions>();\n        var extension = coreOptions.Extensions.First(x => x.Info.IsDatabaseProvider);\n        options.DataSource = extension.GetType().GetProperty(nameof(options.DataSource))?.GetValue(extension) as NpgsqlDataSource;\n        if (options.DataSource == null)\n        {\n            options.ConnectionString = extension.GetType().GetProperty(nameof(options.ConnectionString))?.GetValue(extension) as string;\n        }\n    }\n}","sourceCodeStart":33,"sourceCodeEnd":66,"githubUrl":"https://github.com/dotnetcore/CAP/blob/e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb/src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlOptions.cs#L33-L66","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.UsePostgreSql()\" thrown in dotnetcore/CAP.","triggerScenarios":"Thrown at src/DotNetCore.CAP.PostgreSql/CAP.PostgreSqlOptions.cs:51 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.UsePostgreSql(Configuration.GetConnectionString(\"...\"))).","For transactional publishing inside DbContext, use the CAP storage extension on DbContext options (options.UsePostgreSql().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"}