網(wǎng)站首頁 編程語言 正文
以下文章來源于公眾號:DotNetCore實(shí)戰(zhàn)
一、咨詢區(qū)
Dmitriy
在 ASP.NET Core
中使用GRPC.ASPNETCore
工具包寫 gRPC
服務(wù),想實(shí)現(xiàn) gRPC
的異常全局?jǐn)r截,
代碼如下:
app.UseExceptionHandler(configure => { ? ? configure.Run(async e => ? ? { ? ? ? ? Console.WriteLine("Exception test code"); ? ? }); });
然后注入到 ServiceCollection 容器中:
services.AddMvc(options => { ? ? options.Filters.Add(typeof(BaseExceptionFilter)); });
奇怪的是,這段代碼并不能實(shí)現(xiàn)攔截功能,我是真的不想讓 try-catch 包裹所有的辦法,太痛苦了。
二、回答區(qū)
valentasm
我們可以給 gRPC 添加一個自定義攔截器,先看一下類定義:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Grpc.Core; using Grpc.Core.Interceptors; using Microsoft.Extensions.Logging; namespace Systemx.WebService.Services { ? ? public class ServerLoggerInterceptor : Interceptor ? ? { ? ? ? ? private readonly ILogger<ServerLoggerInterceptor> _logger; ? ? ? ? public ServerLoggerInterceptor(ILogger<ServerLoggerInterceptor> logger) ? ? ? ? { ? ? ? ? ? ? _logger = logger; ? ? ? ? } ? ? ? ? public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>( ? ? ? ? ? ? TRequest request, ? ? ? ? ? ? ServerCallContext context, ? ? ? ? ? ? UnaryServerMethod<TRequest, TResponse> continuation) ? ? ? ? { ? ? ? ? ? ? //LogCall<TRequest, TResponse>(MethodType.Unary, context); ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return await continuation(request, context); ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? // Note: The gRPC framework also logs exceptions thrown by handlers to .NET Core logging. ? ? ? ? ? ? ? ? _logger.LogError(ex, $"Error thrown by {context.Method}."); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? throw; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? ? } }
接下來就可以在 Startup 中通過 AddGrpc 注入啦:
services.AddGrpc(options => { ? ? { ? ? ? ? options.Interceptors.Add<ServerLoggerInterceptor>(); ? ? ? ? options.EnableDetailedErrors = true; ? ? } });
三、點(diǎn)評區(qū)
grpc
早已經(jīng)替代 wcf
成功一種基于tcp
的跨機(jī)器通訊技術(shù),看得出 grpc 和 asp.net core 集成越來越好,是得需要大家花費(fèi)精力好好學(xué)習(xí)。
相關(guān)推薦
- 2024-03-14 Liunx安裝Redis
- 2024-03-13 微信小程序登錄接口 偶發(fā)性 解密錯誤
- 2024-03-14 Linux Nginx自定義安裝目錄
- 2022-09-09 PyTorch策略梯度算法詳情_python
- 2024-03-13 Linux 安裝RabbitMQ
- 2023-10-13 is using incorrect casing. Use PascalCase for Reac
- 2023-03-13 Go語言中GORM存取數(shù)組/自定義類型數(shù)據(jù)_Golang
- 2024-03-13 TypeError: Cannot read property ‘get‘ of undefined
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支