等待 c# 中 grpc 拦截器中的延续方法



我想在 c# .net core 中有一个 grpc 拦截器来检查所有请求凭据并记录所有 rpc 异常,但如果没有 Await 关键字的继续方法调用,则其异常不会在此级别引发。 所以我等待拦截器中的继续以捕获所有超出。到目前为止,所有事情都运行良好,但我不知道它以后是否会导致任何问题,尤其是在多个 cincurrent rpc 调用中?

    public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod<TRequest, TResponse> continuation)
    {
        try
        {
            CheckLogin(context);
            var res = await continuation(request, context);        
            return res;
        }
        catch (RpcException ex)
        {
            _logger.LogError(ex, "RPC Error. UnaryServerHanler Error. Method : {method}", context.Method);
            throw ex;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "UnaryServerHanler Error. Method : {method}", context.Method);
            throw ex;
        }
    }

在执行异步方法时,不必担心使用 await 关键字。

实际上,此机制是为了更好地处理并发代码执行而构建的。

请观看 https://stackoverflow.com/a/14178317/7779827

最新更新