MKS.KeyedLocking.MediatR 1.0.0

dotnet add package MKS.KeyedLocking.MediatR --version 1.0.0
                    
NuGet\Install-Package MKS.KeyedLocking.MediatR -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MKS.KeyedLocking.MediatR" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MKS.KeyedLocking.MediatR" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MKS.KeyedLocking.MediatR" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MKS.KeyedLocking.MediatR --version 1.0.0
                    
#r "nuget: MKS.KeyedLocking.MediatR, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MKS.KeyedLocking.MediatR@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MKS.KeyedLocking.MediatR&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MKS.KeyedLocking.MediatR&version=1.0.0
                    
Install as a Cake Tool

MKS.KeyedLocking.MediatR

MediatR pipeline integration for automatic keyed locking in CQRS handlers.

Features

  • KeyedLockFilter<TRequest, TResponse> - Generic base class per command/query (similar to audit filter patterns)
  • GetAcquireOptions(request) - Build lock key and timeout from the request payload
  • Assembly scanning - AddKeyedLockFilters() discovers and registers all filter subclasses
  • Explicit registration - AddKeyedLockFilter<TFilter, TRequest, TResponse>() for manual setup
  • Backend agnostic - Works with any registered IKeyedLockService

Installation

dotnet add package MKS.KeyedLocking.MediatR

Also install a lock backend:

dotnet add package MKS.KeyedLocking.Redis
dotnet add package MKS.KeyedLocking.Composite

Dependency Injection

builder.Services.AddMediatR(cfg =>
    cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));

builder.Services.AddSingleton<IConnectionMultiplexer>(_ =>
    ConnectionMultiplexer.Connect("localhost:6379"));

builder.Services.AddRedisKeyedLock()
        .AddCompositeKeyedLock();

builder.Services.AddKeyedLockFilters(typeof(Program).Assembly);

Define a Lock Filter

Create one filter class per command or query that needs locking:

public class CreateClientRecipientCommandLock
    : KeyedLockFilter<CreateClientRecipientCommand, ClientRecipientDto>
{
    public CreateClientRecipientCommandLock(IKeyedLockService lockService)
        : base(lockService)
    {
    }

    protected override KeyedLockAcquireOptions GetAcquireOptions(CreateClientRecipientCommand request)
        => new($"client-recipient:{request.ClientId}", TimeSpan.FromSeconds(30));
}

The handler stays lock-free:

public class CreateClientRecipientHandler
    : IRequestHandler<CreateClientRecipientCommand, ClientRecipientDto>
{
    public async Task<ClientRecipientDto> Handle(
        CreateClientRecipientCommand request,
        CancellationToken cancellationToken)
    {
        // Lock already acquired by CreateClientRecipientCommandLock pipeline filter
        // ... business logic only
    }
}

Skip Locking Conditionally

Return null from GetAcquireOptions to skip locking for specific requests:

protected override KeyedLockAcquireOptions? GetAcquireOptions(UpdateCommand request)
    => request.SkipLock ? null : new($"entity:{request.Id}");

Explicit Registration

When you prefer not to scan assemblies:

services.AddKeyedLockFilter<CreateClientRecipientCommandLock, CreateClientRecipientCommand, ClientRecipientDto>();

Requirements

  • .NET 10.0
  • MediatR 13.0+
  • MKS.KeyedLocking.Abstractions 1.0.0+
  • A registered IKeyedLockService implementation
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 136 7/6/2026

Version 1.0.0: Initial release.
      KeyedLockFilter base class with AddKeyedLockFilters assembly scanning.