一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - ASP.NET Core 3.0 gRPC攔截器的使用

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

2020-06-24 15:24曉晨Master ASP.NET教程

這篇文章主要介紹了ASP.NET Core 3.0 gRPC攔截器的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

一. 前言

前面兩篇文章給大家介紹了使用gRPC的入門以及雙向流的使用,今天介紹的是gRPC中的攔截器。攔截器就像MVC的過濾器或者是ASP.NET Core middleware 一樣,具有面向切面的思想,可以在調用服務的時候進行一些統一處理, 很適合在這里處理驗證、日志等流程。本片文章就以記錄日志為例來進行講解。

二. Interceptor 類介紹

Interceptor類是gRPC服務攔截器的基類,是一個抽象類,它定了幾個虛方法,分別如下:

?
1
2
3
4
5
6
7
8
9
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方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
  }
}

注冊攔截器:

?
1
2
3
4
5
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 Core 3.0 gRPC攔截器的使用

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

四. 服務端攔截器

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

我們在服務端需要實現的方法是 UnaryServerHandler

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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)}");
  }
}

注冊攔截器:

?
1
2
3
4
5
6
7
public void ConfigureServices(IServiceCollection services)
{
  services.AddGrpc(options =>
  {
    options.Interceptors.Add<ServerLoggerInterceptor>();
  });
}

運行:

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

可以看到服務端成功攔截到了,客戶端的調用。

五. 參考資料

.NET Core 上的 gRPC 的簡介

本文Demo

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/stulzq/p/11840535.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: chinesegay黑袜玩奴| 羞羞色男人的天堂伊人久久 | 国产精品久久久久影院色老大 | 四虎四虎 | 精品日韩欧美一区二区三区在线播放 | 免费一看一级毛片人 | 日韩网新片免费 | 久久草福利自拍视频在线观看 | 亚洲 欧美 国产 综合 播放 | 华人亚洲欧美精品国产 | 日本天堂视频 | 日韩欧美一区二区在线观看 | 日本色吧 | 236宅宅2021最新理论 | 日本mv精品中文字幕 | 97午夜 | 美女禁区视频无遮挡免费看 | 国产综合图区 | xx18美女美国 | 国产区成人精品视频 | 99re8在线精品视频免费播放 | 成年性生交大片免费看 | 欧美在线视频免费播放 | 国产99er66在线视频 | 色批网站www | www.精品视频 | 狠狠色婷婷丁香六月 | 俄罗斯烧性春三级k8播放 | 久久午夜夜伦痒痒想咳嗽P 久久无码AV亚洲精品色午夜麻豆 | 亚洲精品第一国产综合高清 | 亚飞与亚基国语1080p在线观看 | 精品国产乱码久久久久久免费流畅 | 四虎在线免费 | 国内久久久 | 欧美黑人ⅹxxx片 | 香蕉大久久 | 99久久精品免费看国产 | 成人二区| 99只有精品 | 青柠在线完整高清观看免费 | 亚洲精品综合网 |