ASP.NET Core 3.0 gRPC攔截器的使用
一. 前言
前面兩篇文章給大家介紹了使用gRPC的入門以及雙向流的使用,今天介紹的是gRPC中的攔截器。攔截器就像MVC的過濾器或者是ASP.NET Core middleware 一樣,具有面向切面的思想,可以在調(diào)用服務(wù)的時候進(jìn)行一些統(tǒng)一處理, 很適合在這里處理驗證、日志等流程。本片文章就以記錄日志為例來進(jìn)行講解。
二. Interceptor 類介紹
Interceptor
類是gRPC服務(wù)攔截器的基類,是一個抽象類,它定了幾個虛方法,分別如下:
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 | 攔截異步服務(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ù)器端處理程序 |
在實際使用中,可以根據(jù)自己的需要來使用對應(yīng)的攔截方法。
三. 客戶端攔截器
基于前面兩篇文章使用的Demo。
在客戶端項目新建一個類,命名為 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ù)端項目新建一個類,命名為 ServerLoggerInterceptor
,繼承攔截器基類 Interceptor
。
我們在服務(wù)端需要實現(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)用。
五. 參考資料
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用grpcui測試ASP.NET core的gRPC服務(wù)
- .NET?Core(.NET6)中g(shù)RPC使用實踐
- .Net?Core微服務(wù)rpc框架GRPC通信實際運(yùn)用
- .Net?Core微服務(wù)rpc框架GRPC通信基礎(chǔ)
- 在?ASP.NET?Core?中為?gRPC?服務(wù)添加全局異常處理
- 如何在.NET Core中為gRPC服務(wù)設(shè)計消息文件(Proto)
- .Net Core中使用Grpc的方法
- ASP.NET Core 3.0使用gRPC的具體方法
- 圖析ASP.NET Core引入gRPC服務(wù)模板
- ASP.NET Core中Grpc通信的簡單用法
相關(guān)文章
MVC4制作網(wǎng)站教程第三章 刪除用戶組操作3.4
這篇文章主要為大家詳細(xì)介紹了MVC4制作網(wǎng)站教程,刪除用戶組功能的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08Asp.Net MVC3.0如何項目部署到Win7 64位系統(tǒng)
這篇文章主要介紹了Asp.Net MVC3.0如何項目部署到Win7 64位系統(tǒng)的全部過程,需要的朋友可以參考下2015-10-10.NET使用NPOI實現(xiàn)讀取帶有圖片的excel數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了.NET如何使用NPOI實現(xiàn)讀取帶有圖片的excel數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03asp.net內(nèi)置對象 Response對象使用介紹
這篇文章主要介紹了asp.net內(nèi)置對象:Response對象使用介紹,對Response對象感興趣的小伙伴們可以參考一下2015-11-11asp.net中倒計時自動跳轉(zhuǎn)頁面的實現(xiàn)方法(使用javascript)
本篇文章介紹了,asp.net中倒計時自動跳轉(zhuǎn)頁面的實現(xiàn)方法(使用javascript)。需要的朋友參考下2013-05-05利用ASP.NET MVC和Bootstrap快速搭建個人博客之后臺dataTable數(shù)據(jù)列表
jQuery dataTables 插件是一個優(yōu)秀的表格插件,應(yīng)用非常廣泛,本文給大家介紹利用ASP.NET MVC和Bootstrap快速搭建個人博客之后臺dataTable數(shù)據(jù)列表,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧2016-07-07asp.net中TextBox只能輸入數(shù)字的最簡潔的兩種方法
這篇文章介紹了asp.net中TextBox只能輸入數(shù)字的最簡潔的兩種方法,有需要的朋友可以參考一下2013-11-11