dotnetcore/CAP · error · ArgumentNullException
Value cannot be null. (Parameter 'client')
Error message
Value cannot be null. (Parameter 'client')
What it means
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.
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.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at src/DotNetCore.CAP.MongoDB/IMonitoringApi.MongoDB.cs:26 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/ee77c4ff4aa02bcc.
Report an issue: GitHub.
Appendix: source
Thrown at src/DotNetCore.CAP.MongoDB/IMonitoringApi.MongoDB.cs:26
using DotNetCore.CAP.Internal;
using DotNetCore.CAP.Messages;
using DotNetCore.CAP.Monitoring;
using DotNetCore.CAP.Persistence;
using DotNetCore.CAP.Serialization;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace DotNetCore.CAP.MongoDB;
public class MongoDBMonitoringApi : IMonitoringApi
{
private readonly IMongoDatabase _database;
private readonly MongoDBOptions _options;
private readonly ISerializer _serializer;
public MongoDBMonitoringApi(IMongoClient client, IOptions<MongoDBOptions> options, ISerializer serializer)
{
var mongoClient = client ?? throw new ArgumentNullException(nameof(client));
_options = options.Value ?? throw new ArgumentNullException(nameof(options));
_serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
_database = mongoClient.GetDatabase(_options.DatabaseName);
}
public async Task<MediumMessage?> GetPublishedMessageAsync(long id)
{
var collection = _database.GetCollection<PublishedMessage>(_options.PublishedCollection);
var message = await collection.Find(x => x.Id == id).FirstOrDefaultAsync().ConfigureAwait(false);
return new MediumMessage
{
Added = message.Added,
Origin = _serializer.Deserialize(message.Content)!,
Content = message.Content,
DbId = message.Id.ToString(),
ExpiresAt = message.ExpiresAt,
Retries = message.Retries
};View on GitHub (pinned to e52b8508e5)