{"record":{"id":"a729f3c50a42133c","repo":"LuckyPennySoftware/MediatR","slug":"unknown-strategy-strategy","errorCode":null,"errorMessage":"Unknown strategy: {strategy}","messagePattern":"Unknown strategy: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"samples/MediatR.Examples.PublishStrategies/Publisher.cs","lineNumber":47,"sourceCode":"    {\n        return Publish(notification, DefaultStrategy, default(CancellationToken));\n    }\n\n    public Task Publish<TNotification>(TNotification notification, PublishStrategy strategy)\n    {\n        return Publish(notification, strategy, default(CancellationToken));\n    }\n\n    public Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken)\n    {\n        return Publish(notification, DefaultStrategy, cancellationToken);\n    }\n\n    public Task Publish<TNotification>(TNotification notification, PublishStrategy strategy, CancellationToken cancellationToken)\n    {\n        if (!PublishStrategies.TryGetValue(strategy, out var mediator))\n        {\n            throw new ArgumentException($\"Unknown strategy: {strategy}\");\n        }\n\n        return mediator.Publish(notification, cancellationToken);\n    }\n\n    private Task ParallelWhenAll(IEnumerable<NotificationHandlerExecutor> handlers, INotification notification, CancellationToken cancellationToken)\n    {\n        var tasks = new List<Task>();\n\n        foreach (var handler in handlers)\n        {\n            tasks.Add(Task.Run(() => handler.HandlerCallback(notification, cancellationToken)));\n        }\n\n        return Task.WhenAll(tasks);\n    }\n\n    private Task ParallelWhenAny(IEnumerable<NotificationHandlerExecutor> handlers, INotification notification, CancellationToken cancellationToken)","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/samples/MediatR.Examples.PublishStrategies/Publisher.cs#L29-L65","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npublisher.Publish(pinged, (PublishStrategy)99); // unknown\n// after\npublisher.Publish(pinged, PublishStrategy.ParallelWhenAll);","handlingStrategy":"validation","validationCode":"if (!publisher.PublishStrategies.ContainsKey(strategy))\n    throw new ArgumentOutOfRangeException(nameof(strategy), strategy, \"PublishStrategy not registered in PublishStrategies.\");","typeGuard":"static bool IsKnownStrategy(PublishStrategy s) => Enum.IsDefined(typeof(PublishStrategy), s) && publisher.PublishStrategies.ContainsKey(s);","tryCatchPattern":null,"preventionTips":["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."],"tags":["sample","publish-strategies","enum-validation","configuration"],"backgroundTag":null,"analyzedSha":"916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5","analyzedAt":"2026-08-13T17:57:04.476Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}