asp.net-core – 如何为ASP.NET Core注册和使用MediatR管道处理
发布时间:2020-12-16 03:38:35 所属栏目:asp.Net 来源:网络整理
导读:我正在使用ASP.NET Core,最新的MediatR和MediatR extension for Core’s DI. 我正在尝试建立一个带有验证的管道,using the official blog post.示例is here. 我不明白如何注册/使用该管道类.另一个blog post显示了如何做到这一点,但我认为它适用于AutoFac.
我正在使用ASP.NET Core,最新的MediatR和MediatR
extension for Core’s DI.
我正在尝试建立一个带有验证的管道,using the official blog post.示例is here. 我不明白如何注册/使用该管道类.另一个blog post显示了如何做到这一点,但我认为它适用于AutoFac. 我如何为内置容器执行此操作? 解决方法
你提到的帖子使用MediatR 2.x.
MediatR 3.0不久前发布,并且内置了对管道的支持.我建议你阅读 associated documentation. 简而言之,MediatR现在公开了一个IPipelineBehavior< TRequest,TResponse>,并且在构建处理程序时,MediatR会自动发现您在容器中注册的实例. 这是ASP.NET Core中的样子: public class MyRequest : IRequest<string> { } public class MyRequestHandler : IRequestHandler<MyRequest,string> { public string Handle(MyRequest message) { return "Hello!"; } } public class TracingBehavior<TRequest,TResponse> : IPipelineBehavior<TRequest,TResponse> { public async Task<TResponse> Handle(TRequest request,RequestHandlerDelegate<TResponse> next) { Trace.WriteLine("Before"); var response = await next(); Trace.WriteLine("After"); return response; } } 非常简单,请求,处理程序和执行某些“日志记录”的行为. 注册也非常简单: var services = new ServiceCollection(); services.AddMediatR(typeof(Program)); services.AddTransient(typeof(IPipelineBehavior<,>),typeof(TracingBehaviour<,>)); var provider = services.BuildServiceProvider(); var mediator = provider.GetRequiredService<IMediator>(); var response = await mediator.Send(new MyRequest()); 这只是将开放式通用TracingBehavior注册为IPipelineBehavior的通用实现. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET控件绑定数据源
- asp.net – NLogConfigurationException – 从’System.Str
- asp.net – 两个DLL中都存在“ModelClientValidationRule”
- asp.net-mvc – ASP.NET MVC:模型与MembershipUsers之间的
- iis-7 – 本地语言的布尔值
- asp.net – 为什么当StateProvider不是InProc时,不会引发Se
- asp-classic – 经典ASP – 获取完整的url名称
- 另一个“ASP.NET MVC适合我?”问题
- 如何将ASP.NET母版页存储在公共库中?
- asp.net-mvc – 为什么我要将WCF与MVC一起使用?
推荐文章
站长推荐
热点阅读