LuckyPennySoftware/MediatR · info · ArgumentException

Name cannot be '2'

Error message

Name cannot be '2'

What it means

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.

Source

Thrown at samples/MediatR.Examples.PublishStrategies/SyncPingedHandler.cs:20

using System.Threading;
using System.Threading.Tasks;

namespace MediatR.Examples.PublishStrategies;

public class SyncPingedHandler : INotificationHandler<Pinged>
{
    public SyncPingedHandler(string name)
    {
        Name = name;
    }

    public string Name { get; set; }

    public Task Handle(Pinged notification, CancellationToken cancellationToken)
    {
        if (Name == "2")
        {
            throw new ArgumentException("Name cannot be '2'");
        }

        Console.WriteLine($"[SyncPingedHandler {Name}] {DateTime.Now:HH:mm:ss.fff} : Pinged");
        Thread.Sleep(100);
        Console.WriteLine($"[SyncPingedHandler {Name}] {DateTime.Now:HH:mm:ss.fff} : After pinged");
        return Task.CompletedTask;
    }
}

View on GitHub (pinned to 916ef1b3d6)

Solutions

  1. Construct SyncPingedHandler with any name other than "2" to avoid the throw.
  2. Leave it as-is and handle it through the chosen strategy's exception aggregation (SyncContinueOnException collects into AggregateException; SyncStopOnException rethrows immediately).
  3. Remove the `if (Name == "2")` block if you repurposed this sample for production.

Example fix

// before
new SyncPingedHandler("2") // throws on Handle
// after
new SyncPingedHandler("1") // runs normally
Defensive patterns

Strategy: validation

Validate before calling

if (handler is SyncPingedHandler s && s.Name == "2")
    throw new InvalidOperationException("SyncPingedHandler(\"2\") is a demo fault; use another name.");

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of LuckyPennySoftware/MediatR@916ef1b3d6 (2026-08-13). Data as JSON: /api/errors/fc7a9fa3a13ddd35. Report an issue: GitHub.