LuckyPennySoftware/MediatR · info · ArgumentException
Name cannot be '2'
Error message
Name cannot be '2'
What it means
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.
Source
Thrown at samples/MediatR.Examples.PublishStrategies/AsyncPingedHandler.cs:20
using System.Threading;
using System.Threading.Tasks;
namespace MediatR.Examples.PublishStrategies;
public class AsyncPingedHandler : INotificationHandler<Pinged>
{
public AsyncPingedHandler(string name)
{
Name = name;
}
public string Name { get; set; }
public async Task Handle(Pinged notification, CancellationToken cancellationToken)
{
if (Name == "2")
{
throw new ArgumentException("Name cannot be '2'");
}
Console.WriteLine($"[AsyncPingedHandler {Name}] {DateTime.Now:HH:mm:ss.fff} : Pinged");
await Task.Delay(100).ConfigureAwait(false);
Console.WriteLine($"[AsyncPingedHandler {Name}] {DateTime.Now:HH:mm:ss.fff} : After pinged");
}
}View on GitHub (pinned to 916ef1b3d6)
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.
Example fix
// before
new AsyncPingedHandler("2") // throws on Handle
// after
new AsyncPingedHandler("0") // runs normally Defensive patterns
Strategy: validation
Validate before calling
// Validate the handler name before relying on the sample's fault-injection contract
if (handler is AsyncPingedHandler a && a.Name == "2")
throw new InvalidOperationException("AsyncPingedHandler(\"2\") is a demo fault; use another name."); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of LuckyPennySoftware/MediatR@916ef1b3d6 (2026-08-13).
Data as JSON: /api/errors/94ecd8e7968817d8.
Report an issue: GitHub.