LuckyPennySoftware/MediatR · error · ArgumentException
Unknown strategy: {strategy}
Error message
Unknown strategy: {strategy} What it means
Thrown by the sample Publisher when the requested PublishStrategy enum value is not a key in its PublishStrategies dictionary. The sample's constructor registers all six enum members (SyncContinueOnException, SyncStopOnException, Async, ParallelNoWait, ParallelWhenAll, ParallelWhenAny), so a miss means the dictionary was mutated or an out-of-range enum value was supplied.
Source
Thrown at samples/MediatR.Examples.PublishStrategies/Publisher.cs:47
{
return Publish(notification, DefaultStrategy, default(CancellationToken));
}
public Task Publish<TNotification>(TNotification notification, PublishStrategy strategy)
{
return Publish(notification, strategy, default(CancellationToken));
}
public Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken)
{
return Publish(notification, DefaultStrategy, cancellationToken);
}
public Task Publish<TNotification>(TNotification notification, PublishStrategy strategy, CancellationToken cancellationToken)
{
if (!PublishStrategies.TryGetValue(strategy, out var mediator))
{
throw new ArgumentException($"Unknown strategy: {strategy}");
}
return mediator.Publish(notification, cancellationToken);
}
private Task ParallelWhenAll(IEnumerable<NotificationHandlerExecutor> handlers, INotification notification, CancellationToken cancellationToken)
{
var tasks = new List<Task>();
foreach (var handler in handlers)
{
tasks.Add(Task.Run(() => handler.HandlerCallback(notification, cancellationToken)));
}
return Task.WhenAll(tasks);
}
private Task ParallelWhenAny(IEnumerable<NotificationHandlerExecutor> handlers, INotification notification, CancellationToken cancellationToken)View on GitHub (pinned to 916ef1b3d6)
Solutions
- Pass one of the six defined PublishStrategy values directly rather than a numeric cast.
- If you extended the enum, add the matching `PublishStrategies[NewStrategy] = new CustomMediator(...)` line in the Publisher constructor.
- Do not mutate or clear the public PublishStrategies dictionary after construction.
Example fix
// before publisher.Publish(pinged, (PublishStrategy)99); // unknown // after publisher.Publish(pinged, PublishStrategy.ParallelWhenAll);
Defensive patterns
Strategy: validation
Validate before calling
if (!publisher.PublishStrategies.ContainsKey(strategy))
throw new ArgumentOutOfRangeException(nameof(strategy), strategy, "PublishStrategy not registered in PublishStrategies."); Type guard
static bool IsKnownStrategy(PublishStrategy s) => Enum.IsDefined(typeof(PublishStrategy), s) && publisher.PublishStrategies.ContainsKey(s);
Prevention
- Always pass named PublishStrategy members, never numeric casts.
- If you extend the enum, register the matching CustomMediator in the Publisher constructor.
- Do not mutate the public PublishStrategies dictionary after construction.
When it happens
Trigger: Calling Publish(notification, strategy) with a PublishStrategy value that is either an invalid numeric cast (e.g. (PublishStrategy)99) or whose dictionary entry was removed/cleared after the Publisher constructor ran.
Common situations: Casting an arbitrary int to PublishStrategy; clearing/overwriting the public PublishStrategies dictionary; extending the PublishStrategy enum with a new member but forgetting to register a CustomMediator for it in the constructor.
Related errors
- Name cannot be '2'
- Name cannot be '2'
- No assemblies found to scan. Supply at least one assembly to
- {openBehaviorType.Name} must implement {typeof(IStreamPipeli
- {implementationType.Name} must implement {typeof(IRequestPre
AI-assisted analysis of LuckyPennySoftware/MediatR@916ef1b3d6 (2026-08-13).
Data as JSON: /api/errors/a729f3c50a42133c.
Report an issue: GitHub.