{"record":{"id":"fc7a9fa3a13ddd35","repo":"LuckyPennySoftware/MediatR","slug":"name-cannot-be-2-fc7a9f","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/SyncPingedHandler.cs","lineNumber":20,"sourceCode":"using System.Threading;\nusing System.Threading.Tasks;\n\nnamespace MediatR.Examples.PublishStrategies;\n\npublic class SyncPingedHandler : INotificationHandler<Pinged>\n{\n    public SyncPingedHandler(string name)\n    {\n        Name = name;\n    }\n\n    public string Name { get; set; }\n\n    public 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($\"[SyncPingedHandler {Name}] {DateTime.Now:HH:mm:ss.fff} : Pinged\");\n        Thread.Sleep(100);\n        Console.WriteLine($\"[SyncPingedHandler {Name}] {DateTime.Now:HH:mm:ss.fff} : After pinged\");\n        return Task.CompletedTask;\n    }\n}","sourceCodeStart":2,"sourceCodeEnd":28,"githubUrl":"https://github.com/LuckyPennySoftware/MediatR/blob/916ef1b3d68ccdc96db8f914eaf1b32fc7db52c5/samples/MediatR.Examples.PublishStrategies/SyncPingedHandler.cs#L2-L28","documentation":"Mirror of the AsyncPingedHandler demo throw, but in the synchronous handler variant. It raises ArgumentException when constructed with name \"2\" so the sample can contrast synchronous Thread.Sleep handler failures against async handler failures under each PublishStrategy.","triggerScenarios":"A SyncPingedHandler created with name \"2\" is invoked for a Pinged notification; the `if (Name == \"2\")` check throws before the Console.WriteLine and Thread.Sleep.","commonSituations":"Running/copying the PublishStrategies sample where this handler is registered as one of several named instances, the \"2\" instance being the designated fault-injecting one.","solutions":["Construct SyncPingedHandler with any name other than \"2\" to avoid the throw.","Leave it as-is and handle it through the chosen strategy's exception aggregation (SyncContinueOnException collects into AggregateException; SyncStopOnException rethrows immediately).","Remove the `if (Name == \"2\")` block if you repurposed this sample for production."],"exampleFix":"// before\nnew SyncPingedHandler(\"2\") // throws on Handle\n// after\nnew SyncPingedHandler(\"1\") // runs normally","handlingStrategy":"validation","validationCode":"if (handler is SyncPingedHandler s && s.Name == \"2\")\n    throw new InvalidOperationException(\"SyncPingedHandler(\\\"2\\\") is a demo fault; use another name.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reserve the name \"2\" instance for fault-propagation demonstrations only.","Remove the scripted throw when adapting the sample to production."],"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"}