怎么在ASP.NET中使用 gRPC拦截器

发布时间:2021-03-22 17:17:08 作者:Leah
来源:亿速云 阅读:154

怎么在ASP.NET中使用 gRPC拦截器?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

Interceptor 类介绍

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

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

各个方法作用如下:


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

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

客户端拦截器

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

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

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

public class ClientLoggerInterceptor:Interceptor
{
  public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(
    TRequest request,
    ClientInterceptorContext<TRequest, TResponse> context,
    AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
  {
    LogCall(context.Method);

    return continuation(request, context);
  }

  private void LogCall<TRequest, TResponse>(Method<TRequest, TResponse> 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.LuCatClient(invoker);
var catReply = await catClient.SuckingCatAsync(new Empty());
Console.WriteLine("调用撸猫服务:"+ catReply.Message);

然后运行:

怎么在ASP.NET中使用 gRPC拦截器

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

服务端拦截器

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

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

public class ServerLoggerInterceptor: Interceptor
{
  private readonly ILogger<ServerLoggerInterceptor> _logger;

  public ServerLoggerInterceptor(ILogger<ServerLoggerInterceptor> logger)
  {
    _logger = logger;
  }

  public override Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
    TRequest request,
    ServerCallContext context,
    UnaryServerMethod<TRequest, TResponse> continuation)
  {
    LogCall<TRequest, TResponse>(MethodType.Unary, context);
    return continuation(request, context);
  }

  private void LogCall<TRequest, TResponse>(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<ServerLoggerInterceptor>();
  });
}

运行:

怎么在ASP.NET中使用 gRPC拦截器

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

看完上述内容,你们掌握怎么在ASP.NET中使用 gRPC拦截器的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 「Protocol Buffer」之PB在gRPC中的应用
  2. 在Python中使用gRPC的方法示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net grpc

上一篇:形参和实参如何在Python中使用

下一篇:如何在vue项目中使用图片上传组件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》