• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

ASP.NET Core 3.0 gRPC拦截器的使用

asp 搞代码 4年前 (2022-01-03) 38次浏览 已收录 0个评论

这篇文章主要介绍了ASP.NET Core 3.0 gRPC拦截器的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一. 前言

前面两篇文章给大家介绍了使用gRPC的入门以及双向流的使用,今天介绍的是gRPC中的拦截器。拦截器就像MVC的过滤器或者是ASP.NET Core middleware 一样,具有面向切面的思想,可以在调用服务的时候进行一些统一处理, 很适合在这里处理验证、日志等流程。本片文章就以记录日志为例来进行讲解。

二. Interceptor 类介绍

Interceptor类是gRPC服务拦截器的基类,是一个抽象类,它定了几个虚方法,分别如下:

 public virtual TResponse BlockingUnaryCall(); public virtual AsyncUnaryCall AsyncUnaryCall(); public virtual AsyncServerStreamingCall AsyncServerStreamingCall(); public virtual AsyncClientStreamingCall AsyncClientStreamingCall(); public virtual AsyncDuplexStreamingCall AsyncDuplexStreamingCall(); public virtual Task UnaryServerHandler(); public virtual Task ClientStreamingServerHandler(); public virtual Task ServerStreamingServerHandler(); public virtual Task DuplexStreamingServerHandler();

各个方法作用如下:

方法名称 作用
BlockingUnaryCall 拦截阻塞调用
AsyncUnaryCall 拦截异步调用
AsyncServerStreamingCall 拦截异步服务端流调用
AsyncClientStreamingCall 拦截异步客户端流调用
AsyncDuplexStreamingCall 拦截异步双向流调用
UnaryServerHandler 用于拦截和传入普通调用服务器端处理程序
ClientStreamingServerHandler 用于拦截客户端流调用的服务器端处理程序
ServerStreamingServerHandler 用于拦截服务端流调用的服务器端处理程序
DuplexStreamingServerHandler 用于拦截双向流调用的服务器端处理程序

在实际使用中,可以根据自己的需要来使用对应的拦截方法。

三. 客户端拦截器

基于前面两篇文章使用的Demo。

在客户端项目新建一个类,命名为 ClientLoggerInterceptor,继承拦截器基类 Interceptor

我们在前面使用的Demo,定义了撸猫服务,其中 SuckingCatAsync方法为异步调用,所以我们重写拦截器的 AsyncUnaryCall方法

 public class ClientLoggerInterceptor:Interceptor { public override AsyncUnaryCall AsyncUnaryCall( TRequest request, ClientInterceptorContext context, AsyncUnaryCallContinuation continuation) { LogCall(context.Method); return continuation(request, context); } private void LogCall(Method method) where TRequest : class where TResponse : class { var initialColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"Starting call. Type: {method.Type}. Request: {typeof(TRequest)}. Response: {typeof(TResponse)}"); Console.ForegroundColor = initialColor; } }

注册拦截器:

 var channel = GrpcChannel.ForAddress("https://localhost:5001"); var invoker = channel.Intercept(new ClientLoggerInterceptor()); var catClient = new LuCat.<mark style="color:transparent">来源gaodaimacom搞#^代%!码网</mark>LuCatClient(invoker); var catReply = await catClient.SuckingCatAsync(new Empty()); Console.WriteLine("调用撸猫服务:"+ catReply.Message);

然后运行:

可以看到成功的在客户端拦截到了调用,并记录了调用信息。

四. 服务端拦截器

在服务端项目新建一个类,命名为 ServerLoggerInterceptor,继承拦截器基类 Interceptor

我们在服务端需要实现的方法是 UnaryServerHandler

 public class ServerLoggerInterceptor: Interceptor { private readonly ILogger _logger; public ServerLoggerInterceptor(ILogger logger) { _logger = logger; } public override Task UnaryServerHandler( TRequest request, ServerCallContext context, UnaryServerMethod continuation) { LogCall(MethodType.Unary, context); return continuation(request, context); } private void LogCall(MethodType methodType, ServerCallContext context) where TRequest : class where TResponse : class { _logger.LogWarning($"Starting call. Type: {methodType}. Request: {typeof(TRequest)}. Response: {typeof(TResponse)}"); } }

注册拦截器:

 public void ConfigureServices(IServiceCollection services) { services.AddGrpc(options => { options.Interceptors.Add(); }); }

运行:

可以看到服务端成功拦截到了,客户端的调用。

五. 参考资料

.NET Core 上的 gRPC 的简介

本文Demo

以上就是ASP.NET Core 3.0 gRPC拦截器的使用的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ASP.NET Core 3.0 gRPC拦截器的使用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址