欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

ASP.NET

當(dāng)前位置:主頁 > 網(wǎng)絡(luò)編程 > ASP.NET >

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

來源:本站原創(chuàng)|時(shí)間:2020-01-11|欄目:ASP.NET|點(diǎn)擊: 次

一. 前言

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

二. Interceptor 類介紹

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

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>();

各個(gè)方法作用如下:

方法名稱 作用
BlockingUnaryCall 攔截阻塞調(diào)用
AsyncUnaryCall 攔截異步調(diào)用
AsyncServerStreamingCall 攔截異步服務(wù)端流調(diào)用
AsyncClientStreamingCall 攔截異步客戶端流調(diào)用
AsyncDuplexStreamingCall 攔截異步雙向流調(diào)用
UnaryServerHandler 用于攔截和傳入普通調(diào)用服務(wù)器端處理程序
ClientStreamingServerHandler 用于攔截客戶端流調(diào)用的服務(wù)器端處理程序
ServerStreamingServerHandler 用于攔截服務(wù)端流調(diào)用的服務(wù)器端處理程序
DuplexStreamingServerHandler 用于攔截雙向流調(diào)用的服務(wù)器端處理程序

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

三. 客戶端攔截器

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

在客戶端項(xiàng)目新建一個(gè)類,命名為 ClientLoggerInterceptor,繼承攔截器基類 Interceptor

我們在前面使用的Demo,定義了擼貓服務(wù),其中 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)用擼貓服務(wù):"+ catReply.Message);

然后運(yùn)行:

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

四. 服務(wù)端攔截器

在服務(wù)端項(xiàng)目新建一個(gè)類,命名為 ServerLoggerInterceptor,繼承攔截器基類 Interceptor

我們在服務(wù)端需要實(shí)現(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>();
  });
}

運(yùn)行:

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

五. 參考資料

.NET Core 上的 gRPC 的簡介

本文Demo

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:淺談.Net Core 認(rèn)證系統(tǒng)源碼解析

欄    目:ASP.NET

下一篇:ASP.NET Core3.X 終端中間件轉(zhuǎn)換為端點(diǎn)路由運(yùn)行詳解

本文標(biāo)題:ASP.NET Core 3.0 gRPC攔截器的使用

本文地址:http://mengdiqiu.com.cn/a1/ASP_NET/10819.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有