将泛型方法转换为异步导致泛型参数出现问题

Converting generic method to async causing issues with generic params

在名为 StaticHelper 的静态 class 中,我有以下通用 static 方法:

public static class StaticHelper
{
    public static TResponse GenericMethod<TResponse, TRequest>(TRequest request,
                                                           Func<TRequest, TResponse> method)
    where TRequest  : BaseRequest
    where TResponse : BaseResponse, new()
{
    // ...
}

Func<TRequest, TResponse> method 是由 GenericMethod 调用的方法的名称。 GenericMethod 用作 WCF 方法的包装器以记录 requests/responses,等等:

public override SomeCustomResponse Request(SomeCustomRequest request)
{
    // GenericMethod above called here
    return StaticHelper.GenericMethod(request, ExecuteRequest));
}

private SomeCustomResponse ExecuteRequest(SomeCustomRequest request)
{
    // ...
}

我现在正在尝试创建它的 async 等价物:

public static async Task<TResponse> GenericMethodAsync<TResponse, TRequest>(TRequest request,
                                                           Func<TRequest, TResponse> method)
    where TRequest  : BaseRequest
    where TResponse : BaseResponse, new()
{
    // ...
}

// i have removed the override keyword here as I don't need it
public async Task<SomeCustomResponse> Request(SomeCustomRequest request)
{
    // GenericMethodAsync above called here
    return await StaticHelper.GenericMethodAsync(request, ExecuteRequest));
}

private async Task<SomeCustomResponse> ExecuteRequest(SomeCustomRequest request)
{
    // ...
}

最后出现两个错误:

public async Task<SomeCustomResponse> Request(SomeCustomRequest request)中(第二种异步方法):

1) The type Task<SomeCustomResponse> cannot be used as type parameter 'TResponse' in the generic type or method 'StaticHelper.GenericMethodAsync<TResponse, TRequest>(TRequest, Func<TRequest, TResponse>)'. There is no implicit reference conversion from Task<SomeCustomResponse> to BaseResponse

...和:

2) Task<SomeCustomResponse> must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TResponse' in the generic type or method StaticHelper.GenericMethodAsync<TResponse, TRequest>(TRequest, Func<TRequest, TResponse>)

更新:下面 René 的回答使错误消失了。我现在有一个新的:

Cannot implicitly convert type 'Task<TResponse>' to 'TResponse'

有问题的行在 StaticHelper.GenericMethodAsync 中,它试图执行 Func:

var response = method(request); // <-- Cannot implicitly convert type 'Task<TResponse>' to 'TResponse'

...显然,解决方案就是 await 即:

var response = await method(request);

您需要更改 GenericMethodAsync 的声明,因为 method (ExecuteRequest) 的 return 类型现在是 Task<TResponse> 而不是 TResponse:

public static async Task<TResponse> GenericMethodAsync<TResponse, TRequest>(
                     TRequest request,
                     Func<TRequest, Task<TResponse>> method) // <-- change here
                            where TRequest  : BaseRequest
                            where TResponse : BaseResponse, new()
{
    // ...
}

并考虑将 ExecuteRequest 重命名为 ExecuteRequestAsync

当然,您现在必须相应地更改 GenericMethodAsyncmethod 的使用:

var response = await method(request);