{"record":{"id":"94ecd8e7968817d8","repo":"LuckyPennySoftware/MediatR","slug":"name-cannot-be-2","errorCode":null,"errorMessage":"Name cannot be '2'","messagePattern":"Name cannot be '2'","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"info","filePath":"samples/MediatR.Examples.PublishStrategies/AsyncPingedHandler.cs","lineNumber":20,"sourceCode":"using System.Threading;\nusing System.Threading.Tasks;\n\nnamespace MediatR.Examples.PublishStrategies;\n\npublic class AsyncPingedHandler : INotificationHandler<Pinged>\n{\n    public AsyncPingedHandler(string name)\n    {\n        Name = name;\n    }\n\n    public string Name { get; set; }\n\n    public async Task Handle(Pinged notification, CancellationToken cancellationToken)\n    {\n        if (Name == \"2\")\n        {\n            throw new ArgumentException(\"Name cannot be '2'\");\n        }\n\n        Console.WriteLine($\"[AsyncPingedHandler {Name}] {DateTime.Now:HH:mm:ss.fff} : Pinged\");\n        await Task.Delay(100).ConfigureAwait(false);\n        Console.WriteLine($\"[AsyncPingedHandler {Name}] {DateTime.Now:HH:mm:ss.fff} : After pinged\");\n    }\n}","sourceCodeStart":2,"sourceCodeEnd":27,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/samples/MediatR.Examples.PublishStrategies/AsyncPingedHandler.cs#L2-L27","documentation":"Intentional demo throw inside a sample notification handler. The sample deliberately raises ArgumentException when the handler was constructed with the name \"2\", to showcase how each PublishStrategy (sync/async/parallel) propagates handler exceptions. It is not a library invariant; it is a scripted fault injected for the PublishStrategies sample.","triggerScenarios":"An instance of AsyncPingedHandler created with the constructor argument \"2\" is resolved and invoked by MediatR when a Pinged notification is published. The check `if (Name == \"2\")` fires on entry to Handle before any logging or delay.","commonSituations":"Running or copying the MediatR.Examples.PublishStrategies sample where handlers are wired with names \"0\",\"1\",\"2\",...; this handler is expected to throw so you can observe how ParallelWhenAll / SyncStopOnException etc. aggregate the failure.","solutions":["If you only want successful runs, construct AsyncPingedHandler with a name other than \"2\" (or remove that registration from the sample's DI setup).","If you are studying error propagation, leave it as-is and await the Publish call so the strategy surfaces the exception (AggregateException for WhenAll, first exception for StopOnException).","If you adapted this sample into production code, delete the `if (Name == \"2\")` block entirely; it exists purely for the demo."],"exampleFix":"// before\nnew AsyncPingedHandler(\"2\") // throws on Handle\n// after\nnew AsyncPingedHandler(\"0\") // runs normally","handlingStrategy":"validation","validationCode":"// Validate the handler name before relying on the sample's fault-injection contract\nif (handler is AsyncPingedHandler a && a.Name == \"2\")\n    throw new InvalidOperationException(\"AsyncPingedHandler(\\\"2\\\") is a demo fault; use another name.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat the name == \"2\" instance as the sample's designated fault-injector and never register it in production paths.","When porting the PublishStrategies sample to production, delete the scripted `if (Name == \\\"2\\\")` blocks."],"tags":["sample","demo-throw","publish-strategies","notification-handler"],"backgroundTag":null,"analyzedSha":"916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5","analyzedAt":"2026-08-13T17:57:04.476Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}