我创建了自定义委托处理程序并覆盖它的方法
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
现在在那个方法中我运行自定义同步方法,你们能告诉我这个例子中哪个是正确的,或者如果两个都不正确,让我知道更好的用法,以避免任何死锁
1.
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (Processor.CanProcess(request))
{
var tsc = new TaskCompletionSource<HttpResponseMessage>(cancellationToken);
tsc.SetResult(Processor.Process(request));
return tsc.Task;
}
return base.SendAsync(request, cancellationToken);
}
2.
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (Processor.CanProcess(request))
return Task.Run(() => Processor.Process(request), cancellationToken);
return base.SendAsync(request, cancellationToken);
}
我应该使用异步等待还是最好保持原样。