【问题标题】:ASPNET CORE GRPC async interceptor methodASPNET CORE GRPC 异步拦截器方法
【发布时间】:2020-12-04 01:39:57
【问题描述】:

我正在尝试异步创建一个新的 AsyncUnaryCall 实例,该实例包装了原始接收的实例。

问题是拦截器基类的设计不佳,它不允许异步方法拦截器。 (如此处所述:https://github.com/grpc/grpc-dotnet/issues/694

为了得到这个想法,这是想要实现的目标:

public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
{
    var result = await MyAsyncStuff(); // if we can not await, what is the alternative?

    return new AsyncUnaryCall<TResponse>(paramters);
}

【问题讨论】:

    标签: c# asp.net-core grpc-dotnet


    【解决方案1】:
        public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, AsyncUnaryCallContinuation<TRequest, TResponse> continuation)
        {
            var response = continuation(request, context);
            var task = MyAsyncStuff(response);
            return new AsyncUnaryCall<TResponse>(task, response.ResponseHeadersAsync, response.GetStatus, response.GetTrailers, response.Dispose);
        }
    
        private async Task<TResponse> MyAsyncStuff<TResponse>(AsyncUnaryCall<TResponse> responseAsync)
        {
            return await responseAsync;
        }
    

    related discussion

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 2021-05-11
      • 2018-03-31
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      相关资源
      最近更新 更多