SH.Mediator 2.0.0

dotnet add package SH.Mediator --version 2.0.0
                    
NuGet\Install-Package SH.Mediator -Version 2.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="SH.Mediator" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SH.Mediator" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SH.Mediator" />
                    
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 SH.Mediator --version 2.0.0
                    
#r "nuget: SH.Mediator, 2.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 SH.Mediator@2.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=SH.Mediator&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SH.Mediator&version=2.0.0
                    
Install as a Cake Tool

安装 NuGet 包

dotnet add package SHMediator

注册服务

using SHMediator;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddSHMediator(typeof(Program).Assembly, options =>
{
    options.UseLoggingInterceptor = true;
    options.UseValidationInterceptor = true;
});

三种 接口

1. IRequest       --  IRequestHandler           请求无应答
2. IRequest<,>    --  IRequestHandler<,>        请求有应答
3. INotification  --  INotificationHandler      通知/广播

1. IRequest -- IRequestHandler

public class MyRequest : IRequest
{
    public string Name { get; set; }
}
public class MyRequestHandler : IRequestHandler<MyRequest>
{
    public Task Handle(MyRequest request, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Hello, {request.Name}!");
        return Task.CompletedTask;
    }
}
// 使用
var mediator = serviceProvider.GetRequiredService<IMediator>();
await mediator.Send(new MyRequest { Name = "World" }); // 输出: Hello, World!

2. IRequest<,> -- IRequestHandler<,>

public class MyRequest : IRequest<MyResponse>
{
    public string Name { get; set; }
}
public class MyResponse
{
    public string Message { get; set; }
}
public class MyRequestHandler : IRequestHandler<MyRequest, MyResponse>
{
    public Task<MyResponse> Handle(MyRequest request, CancellationToken cancellationToken)
    {
        return Task.FromResult(new MyResponse { Message = $"Hello, {request.Name}!" });
    }
}
// 使用
var mediator = serviceProvider.GetRequiredService<IMediator>();
var response = await mediator.Send(new MyRequest { Name = "World" });
Console.WriteLine(response.Message); // 输出: Hello, World!

3. INotification -- INotificationHandler

public class MyNotify : INotification
{
    public string Name { get; set; }
}
public class MyNotify01Handlder : INotificationHandler<MyNotify>
{
     public Task Handle(MyNotify notification, CancellationToken cancellationToken)
     {
         Console.WriteLine($"Notification 01 received: {notification.Name}");
         return Task.CompletedTask;
     }
}
public class MyNotify02Handlder : INotificationHandler<MyNotify>
{
     public Task Handle(MyNotify notification, CancellationToken cancellationToken)
     {
         Console.WriteLine($"Notification 02 received: {notification.Name}");
         return Task.CompletedTask;
     }
}
// 使用
var mediator = serviceProvider.GetRequiredService<IMediator>();
await mediator.Publish(new MyNotify { Name = "Test Notification" });
// 输出: Notification 01 received: Test Notification
// 输出: Notification 02 received: Test Notification

文档目录

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
2.0.0 111 7/27/2026
1.1.9 133 1/30/2026
1.1.8 131 1/23/2026
1.1.7 134 1/23/2026
1.1.6 134 1/22/2026
1.1.5 141 1/22/2026
1.1.4 146 1/22/2026
1.1.3 130 1/22/2026
1.0.1 130 1/21/2026
1.0.0 133 1/21/2026