dotnetcore/CAP · error · ArgumentNullException
Value cannot be null. (Parameter 'topics')
Error message
Value cannot be null. (Parameter 'topics')
What it means
SubscribeAsync requires a non-null collection of topic names to create SNS topics/subscribe SQS queues. It throws ArgumentNullException when topics is null, distinguishing 'null' (bug) from an empty list (no subscriptions).
Solutions
- Pass a non-null collection; use Array.Empty<string>() or new List<string>() when there is nothing to subscribe
- Verify consumers/subscribers are registered via services.AddCap(x => x.AddSubscribe(...)) before bootstrapping
- Guard the call site: if (topics != null) await SubscribeAsync(topics)
- Catch ArgumentNullException at startup and log which consumer client failed
Example fix
// before await sqsConsumer.SubscribeAsync(GetTopics()); // GetTopics() returns null // after var topics = GetTopics() ?? Enumerable.Empty<string>(); await sqsConsumer.SubscribeAsync(topics);
Defensive patterns
Strategy: validation
Validate before calling
if (topics is null) throw new InvalidOperationException("topics must not be null; pass an empty collection for none"); Type guard
bool IsValidTopics(IEnumerable<string>? t) => t is not null;
Try / catch
try { await client.SubscribeAsync(topics); } catch (ArgumentNullException ex) when (ex.ParamName == "topics") { logger.LogError(ex, "SubscribeAsync received null topics"); throw; } Prevention
- Always register subscribers via AddSubscribe so CAP builds the topic list
- Default null topic lists to empty enumerables at the config boundary
When it happens
Trigger: Calling AmazonSQSConsumerClient.SubscribeAsync(null) directly, or CAP's bootstrapper invoking SubscribeAsync with a null subscriber/topic collection when no consumers were registered but the collection itself was never initialized.
Common situations: Custom IConsumerClient implementations or forks of CAP where the topic collection is built from a dictionary lookup that returned null; also seen when upgrading CAP versions and the startup path changed from empty-list to null semantics.
Related errors
- Value cannot be null. (Parameter 'topicNames')
- Value cannot be null. (Parameter 'configure')
- Value cannot be null. (Parameter 'topics')
- Value cannot be null. (Parameter 'options')
- Value cannot be null. (Parameter 'connectionString')
AI-assisted analysis of dotnetcore/CAP@e52b8508e5 (2026-09-14).
Data as JSON: /api/errors/70d07945dc33623e.
Report an issue: GitHub.
Appendix: source
Thrown at src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs:71
var topicArns = new List<string>();
foreach (var topic in topicNames)
{
var createTopicRequest = new CreateTopicRequest(topic.NormalizeForAws());
var createTopicResponse = await _snsClient!.CreateTopicAsync(createTopicRequest).ConfigureAwait(false);
topicArns.Add(createTopicResponse.TopicArn);
}
await GenerateSqsAccessPolicyAsync(topicArns).ConfigureAwait(false);
return topicArns;
}
public async Task SubscribeAsync(IEnumerable<string> topics)
{
if (topics == null) throw new ArgumentNullException(nameof(topics));
await ConnectAsync().ConfigureAwait(false);
await SubscribeToTopics(topics).ConfigureAwait(false);
}
public async Task ListeningAsync(TimeSpan timeout, CancellationToken cancellationToken)
{
await ConnectAsync().ConfigureAwait(false);
var request = new ReceiveMessageRequest(_queueUrl)
{
WaitTimeSeconds = 5,
MaxNumberOfMessages = 1
};
while (true)
{View on GitHub (pinned to e52b8508e5)