在C#中使用通用约束返回Task的异步方法
作者:互联网
我已经在a project I’m working on中实现了命令模式.这几乎是当前结构:
public class Response
{
public bool Success { get; private set; }
public static Response CreateErrorResponse()
{
return new Response { Success = false };
}
}
public interface ICommand<T> where T : Response
{
Task<T> ExecuteAsync();
}
public abstract CommandBase : ICommand<T> where T: Response
{
protected abstract Uri BuildUrl();
protected abstract Task<T> HandleResponseAsync();
public async override Task<T> ExecuteAsync()
{
var url = BuildUrl();
var httpClient = new HttpClient();
var response = await httpClient.GetAsync(url);
return await HandleResponseAsync(response);
}
}
我想处理HttpClient可能引发的任何异常,因此我想将CommandBase.ExecuteAsync更改为类似的内容…
public async override Task<T> ExecuteAsync()
{
var url = BuildUrl();
var httpClient = new HttpClient();
try
{
var response = await httpClient.GetAsync(url);
return await HandleResponseAsync(response);
}
catch (HttpRequestException hex)
{
return Response.CreateErrorResponse(); // doesn't compile
}
}
我得到的编译错误是“无法将类型Response转换为异步返回类型T”.如in this question所述,我无法使用T.CreateErrorResponse().
我该如何解决?
编辑下降投票者:您是否同意在这样的库中捕获异常,问题仍然存在!
解决方法:
尽管我不确定这是否是最佳解决方案(或在您的特定用例中是否可行),但是您可以执行以下操作:
public class Response
{
public bool Success { get; private set; }
public ExceptionDispatchInfo ErrorInfo { get; private set; }
public bool HasFailed
{
get { return !Success; }
}
public static T CreateErrorResponse<T>(ExceptionDispatchInfo errorInfo) where T : Response, new()
{
var response = new T();
response.Success = false;
response.ErrorInfo = errorInfo;
return response;
}
}
用法:
catch (HttpRequestException hex)
{
return Response.CreateErrorResponse<T>(ExceptionDispatchInfo.Capture(hex)); // should compile (I did not check)
}
标签:command-pattern,async-await,generics,c 来源: https://codeday.me/bug/20191122/2062798.html