csharp MediatR.Net示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp MediatR.Net示例相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
namespace MediatRExample
{
public static class Program
{
public static void Main(string[] args)
{
var services = new ServiceCollection();
//必须要注册的两个工厂
services.AddScoped<SingleInstanceFactory>(p => p.GetRequiredService);
services.AddScoped<MultiInstanceFactory>(p => p.GetRequiredServices);
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(MyBehavior<,>));
//查找程序集中所有类,把他们与自己的接口注册
services.Scan(scan => scan //scan方法是第三方组件Scrutor的方法
.FromAssembliesOf(typeof(IMediator), typeof(MyRequest))
.AddClasses()
.AsImplementedInterfaces());
var mediator = services.BuildServiceProvider().GetService<IMediator>();
var result = mediator.Send(new MyRequest() { Message = "message" }).Result;//send 是异步的
Console.Write(result.Result);
Console.ReadKey();
}
private static IEnumerable<object> GetRequiredServices(this IServiceProvider provider, Type serviceType)
{
return (IEnumerable<object>)provider.GetRequiredService(typeof(IEnumerable<>).MakeGenericType(serviceType));
}
}
public class MyBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
Console.WriteLine("do something in MyBehavior");
var response = await next();
return response;
}
}
public class MyRequestHandler : IRequestHandler<MyRequest, MyResponse>
{
public Task<MyResponse> Handle(MyRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(new MyResponse() { Result = "the message from request:" + request.Message });
}
}
public class MyRequest : IRequest<MyResponse>
{
public string Message { get; set; }
}
public class MyResponse
{
public string Result { get; set; }
}
public class MyResponseWrap
{
public string Result { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
namespace MediatRExample
{
public static class Program
{
public static void Main(string[] args)
{
var services = new ServiceCollection();
//必须要注册的两个工厂
services.AddScoped<SingleInstanceFactory>(p => p.GetRequiredService);
services.AddScoped<MultiInstanceFactory>(p => p.GetRequiredServices);
//查找程序集中所有类,把他们与自己的接口注册
services.Scan(scan => scan //scan方法是第三方组件Scrutor的方法
.FromAssembliesOf(typeof(IMediator), typeof(MyNotify))
.AddClasses()
.AsImplementedInterfaces());
var mediator = services.BuildServiceProvider().GetService<IMediator>();
mediator.Publish(new MyNotify() { Message = "message" }).Wait();//Publish 是异步的,不需要返回值
Console.ReadKey();
}
private static IEnumerable<object> GetRequiredServices(this IServiceProvider provider, Type serviceType)
{
return (IEnumerable<object>)provider.GetRequiredService(typeof(IEnumerable<>).MakeGenericType(serviceType));
}
}
public class MyNotify : INotification
{
public string Message { get; set; }
}
public class MyNotifyHandler : INotificationHandler<MyNotify>
{
public Task Handle(MyNotify notification, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(MyNotifyHandler)} invoked,message:{notification.Message}");
return Task.CompletedTask;
}
}
public class MyNotifyHandler2 : INotificationHandler<MyNotify>
{
public Task Handle(MyNotify notification, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(MyNotifyHandler2)} invoked,message:{notification.Message}");
return Task.CompletedTask;
}
}
}
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
namespace MediatRExample
{
public static class Program
{
public static void Main(string[] args)
{
var services = new ServiceCollection();
//必须要注册的两个工厂
services.AddScoped<SingleInstanceFactory>(p => p.GetRequiredService);
services.AddScoped<MultiInstanceFactory>(p => p.GetRequiredServices);
//查找程序集中所有类,把他们与自己的接口注册
services.Scan(scan => scan //scan方法是第三方组件Scrutor的方法
.FromAssembliesOf(typeof(IMediator), typeof(MyRequest))
.AddClasses()
.AsImplementedInterfaces());
var mediator = services.BuildServiceProvider().GetService<IMediator>();
var result = mediator.Send(new MyRequest() { Message = "message" }).Result;//send 是异步的
Console.Write(result.Result);
Console.ReadKey();
}
private static IEnumerable<object> GetRequiredServices(this IServiceProvider provider, Type serviceType)
{
return (IEnumerable<object>)provider.GetRequiredService(typeof(IEnumerable<>).MakeGenericType(serviceType));
}
}
public class MyRequestHandler : IRequestHandler<MyRequest, MyResponse>
{
public Task<MyResponse> Handle(MyRequest request, CancellationToken cancellationToken)
{
return Task.FromResult(new MyResponse() { Result = "the message from request:" + request.Message });
}
}
public class MyRequest : IRequest<MyResponse>
{
public string Message { get; set; }
}
public class MyResponse
{
public string Result { get; set; }
}
}
以上是关于csharp MediatR.Net示例的主要内容,如果未能解决你的问题,请参考以下文章
csharp 示例 - CSHARP - 数据层,GetCustomerOrderDataJson.cs
csharp 示例 - CSHARP - 数据层,GetProductsDataJson.cs
csharp 示例 - CSHARP - 数据层,GetSingleCustomerDataJson.cs
csharp 示例 - CSHARP - 报告 - GenerateReport-GenerateBubbleChartFromJsoninExcel.cs
csharp 示例 - CSHARP - 报告 - GenerateReport-GenerateBubbleChartFromJsoninPresentation.cs
csharp 示例 - CSHARP - 报告 - GenerateReport-GenerateBubbleChartFromJsoninWord.cs