MediatR

repository·main·Indexed 11 days ago

https://github.com/luckypennysoftware/mediatr

A simple, in-process messaging implementation for .NET that supports request/response, commands, queries, notifications, and events using C# generic variance. It integrates with Microsoft.Extensions.DependencyInjection and provides a pipeline that can be extended with behaviors, stream behaviors, pre-processors, and post-processors.

Tokens
1.1K
Snippets
4
Records
6
Agent score
46%

What's inside MediatR

  1. Configure MediatR license via environment variables

    main

    MediatR supports auto-discovery of license keys via environment variables, which is ideal for containerized or cloud environments. The key is resolved using the following order of precedence (the first one found is used):

    1. Explicit value set in code (cfg.LicenseKey or Mediator.LicenseKey).
    2. The MEDIATR_LICENSE_KEY environment variable.
    3. The LUCKYPENNY_LICENSE_KEY environment variable (a shared key for Lucky Penny products).

    No code changes are required if using environment variables; simply register MediatR as usual.

  2. Install MediatR via NuGet

    main

    Install MediatR using the NuGet Package Manager or the .NET Core CLI. This will install the core MediatR package and all required dependencies.

    # Using Package Manager Console
    Install-Package MediatR
    
    # Using .NET Core CLI
    dotnet add package MediatR
  3. Register MediatR with IServiceCollection

    main

    MediatR integrates with Microsoft.Extensions.DependencyInjection. You can register services and handlers by scanning assemblies.

    When you call AddMediatR, the following are registered as transient:

    • IMediator
    • ISender
    • IPublisher
    • IRequestHandler<,> and IRequestHandler<> implementations
    • INotificationHandler<> implementations
    • IStreamRequestHandler<> implementations
    • IRequestExceptionHandler<,,> implementations
    • IRequestExceptionAction<,> implementations

    It also registers open generic implementations for INotificationHandler<>, IRequestExceptionHandler<,,>, and IRequestExceptionAction<,>.

    // Register using a type within the assembly containing your handlers
    services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Startup>());
    
    // Or register using the assembly directly
    services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Startup).Assembly));
  4. Use the MediatR.Contracts package

    main

    If you need to reference only the MediatR interfaces without the full implementation (e.g., in API contracts, gRPC contracts, or Blazor projects where contracts reside in a separate assembly from handlers), install the MediatR.Contracts package.

    This package includes:

    • IRequest (and generic variants)
    • INotification
    • IStreamRequest
  5. Set the MediatR license key

    main

    MediatR requires a license key. You can set it explicitly in code during registration or via static assignment.

    Note: Client applications like Blazor WASM do not need to set a license key. To suppress license warnings in your logs, configure your logging filter: builder.Logging.AddFilter("LuckyPennySoftware.MediatR.License", LogLevel.None);

    // Option 1: During IServiceCollection registration
    services.AddMediatR(cfg => 
    {
        cfg.LicenseKey = "<license key here>";
    });
    
    // Option 2: Static assignment (if not using Microsoft.Extensions.DependencyInjection)
    Mediator.LicenseKey = "<license key here>";
  6. Register MediatR behaviors and processors

    main

    You can extend the MediatR pipeline by registering behaviors, stream behaviors, pre-processors, and post-processors within the AddMediatR configuration block.

    services.AddMediatR(cfg => {
        cfg.RegisterServicesFromAssembly(typeof(Startup).Assembly);
        cfg.AddBehavior<PingPongBehavior>();
        cfg.AddStreamBehavior<PingPongStreamBehavior>();
        cfg.AddRequestPreProcessor<PingPreProcessor>();
        cfg.AddRequestPostProcessor<PingPongPostProcessor>();
        cfg.AddOpenBehavior(typeof(GenericBehavior<,>));
    });