溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2021-03-22 17:17:08 來源:億速云 閱讀:167 作者:Leah 欄目:開發(fā)技術(shù)

怎么在ASP.NET中使用 gRPC攔截器?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

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攔截阻塞調(diào)用
AsyncUnaryCall攔截異步調(diào)用
AsyncServerStreamingCall攔截異步服務端流調(diào)用
AsyncClientStreamingCall攔截異步客戶端流調(diào)用
AsyncDuplexStreamingCall攔截異步雙向流調(diào)用
UnaryServerHandler用于攔截和傳入普通調(diào)用服務器端處理程序
ClientStreamingServerHandler用于攔截客戶端流調(diào)用的服務器端處理程序
ServerStreamingServerHandler用于攔截服務端流調(diào)用的服務器端處理程序
DuplexStreamingServerHandler用于攔截雙向流調(diào)用的服務器端處理程序

在實際使用中,可以根據(jù)自己的需要來使用對應的攔截方法。

客戶端攔截器

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

在客戶端項目新建一個類,命名為 ClientLoggerInterceptor,繼承攔截器基類 Interceptor。

我們在前面使用的Demo,定義了擼貓服務,其中 SuckingCatAsync方法為異步調(diào)用,所以我們重寫攔截器的 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("調(diào)用擼貓服務:"+ catReply.Message);

然后運行:

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

可以看到成功的在客戶端攔截到了調(diào)用,并記錄了調(diào)用信息。

服務端攔截器

在服務端項目新建一個類,命名為 ServerLoggerInterceptor,繼承攔截器基類 Interceptor。

我們在服務端需要實現(xiàn)的方法是 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攔截器

可以看到服務端成功攔截到了,客戶端的調(diào)用。

看完上述內(nèi)容,你們掌握怎么在ASP.NET中使用 gRPC攔截器的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI