<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何在 ASP.NET Core 中為 gRPC 服務(wù)添加全局異常處理 ?

          共 1810字,需瀏覽 4分鐘

           ·

          2021-12-15 08:37

          咨詢區(qū)

          • Dmitriy

          我在 ASP.NET Core 中使用 GRPC.ASPNETCore 工具包寫(xiě) gRPC 服務(wù),現(xiàn)在我想實(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 添加一個(gè)自定義攔截器,先看一下類定義。


          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?_logger;

          ????????public?ServerLoggerInterceptor(ILogger?logger)
          ????????{
          ????????????_logger?=?logger;
          ????????}

          ????????public?override?async?Task?UnaryServerHandler(
          ????????????TRequest?request,
          ????????????ServerCallContext?context,
          ????????????UnaryServerMethod?continuation)
          ????????{
          ????????????//LogCall(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;
          ????????????}
          ????????}
          ???????
          ????}
          }

          接下來(lái)就可以在 Startup 中通過(guò) AddGrpc 注入啦。


          services.AddGrpc(options?=>
          {
          ????{
          ????????options.Interceptors.Add();
          ????????options.EnableDetailedErrors?=?true;
          ????}
          });

          點(diǎn)評(píng)區(qū)

          grpc 早已經(jīng)替代 wcf 成功一種基于tcp的跨機(jī)器通訊技術(shù),看得出 grpc 和 asp.net core 集成越來(lái)越好,是得需要大家花費(fèi)精力好好學(xué)習(xí)。

          瀏覽 39
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天爽夜爽 | 女人18片毛片90分钟 | 婷婷五月丁香久久 | 黄色片免费国产 | 欧.美干逼逼 |