{"record":{"id":"ee77c4ff4aa02bcc","repo":"dotnetcore/CAP","slug":"value-cannot-be-null-parameter-client","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'client')","messagePattern":"Value cannot be null\\. \\(Parameter 'client'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/DotNetCore.CAP.MongoDB/IMonitoringApi.MongoDB.cs","lineNumber":26,"sourceCode":"using DotNetCore.CAP.Internal;\nusing DotNetCore.CAP.Messages;\nusing DotNetCore.CAP.Monitoring;\nusing DotNetCore.CAP.Persistence;\nusing DotNetCore.CAP.Serialization;\nusing Microsoft.Extensions.Options;\nusing MongoDB.Driver;\n\nnamespace DotNetCore.CAP.MongoDB;\n\npublic class MongoDBMonitoringApi : IMonitoringApi\n{\n    private readonly IMongoDatabase _database;\n    private readonly MongoDBOptions _options;\n    private readonly ISerializer _serializer;\n\n    public MongoDBMonitoringApi(IMongoClient client, IOptions<MongoDBOptions> options, ISerializer serializer)\n    {\n        var mongoClient = client ?? throw new ArgumentNullException(nameof(client));\n        _options = options.Value ?? throw new ArgumentNullException(nameof(options));\n        _serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));\n        _database = mongoClient.GetDatabase(_options.DatabaseName);\n    }\n\n    public async Task<MediumMessage?> GetPublishedMessageAsync(long id)\n    {\n        var collection = _database.GetCollection<PublishedMessage>(_options.PublishedCollection);\n        var message = await collection.Find(x => x.Id == id).FirstOrDefaultAsync().ConfigureAwait(false);\n        return new MediumMessage\n        {\n            Added = message.Added,\n            Origin = _serializer.Deserialize(message.Content)!,\n            Content = message.Content,\n            DbId = message.Id.ToString(),\n            ExpiresAt = message.ExpiresAt,\n            Retries = message.Retries\n        };","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/dotnetcore/CAP/blob/e52b8508e54cdb7a9ce7f9fec03d9ea8ad2710fb/src/DotNetCore.CAP.MongoDB/IMonitoringApi.MongoDB.cs#L8-L44","documentation":"Thrown from the MongoDBMonitoringApi constructor when the injected IMongoClient is null. The constructor immediately dereferences 'client' to resolve the database, so a null client — typically because MongoClient was never registered in DI before AddCAP MongoDB monitoring was resolved — fails instantly with ArgumentNullException.","triggerScenarios":"Thrown at src/DotNetCore.CAP.MongoDB/IMonitoringApi.MongoDB.cs:26 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Register an IMongoClient in the service collection (e.g. services.AddSingleton<IMongoClient>(new MongoClient(connectionString))) before CAP resolves MongoDBMonitoringApi.","Verify the CAP MongoDB registration was not made with a null client instance (services.AddCAP(x => x.UseMongoDB(client, db)) must receive a non-null client).","Check for a DI scope/lifetime mismatch where a manually constructed MongoDBMonitoringApi is passed null instead of resolving it from the provider."],"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"}