1

我正在尝试异步创建一个新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);
}
4

1 回答 1

0
    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;
    }

相关讨论

于 2020-12-03T17:39:57.313 回答