我试图在我的IHttpAsyncHandler中调用一个IHttpAsyncHandler,我发现有一个类似于使用IHttpAsyncHandler异步调用WebService的答案
我对答案有疑问。如果有人能帮我我很感激。
它有
Task webClientDownloadTask = webClientDownloadCompletionSource.Task;我的问题是
以下是完整的代码:
public class MyAsyncHandler : IHttpAsyncHandler
{
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
// NOTE: the result of this operation is void, but TCS requires some data type so we just use bool
TaskCompletionSource<bool> webClientDownloadCompletionSource = new TaskCompletionSource<bool>();
WebClient webClient = new WebClient())
HttpContext currentHttpContext = HttpContext.Current;
// Setup the download completed event handler
client.DownloadDataCompleted += (o, e) =>
{
if(e.Cancelled)
{
// If it was canceled, signal the TCS is cacnceled
// NOTE: probably don't need this since you have nothing canceling the operation anyway
webClientDownloadCompletionSource.SetCanceled();
}
else if(e.Error != null)
{
// If there was an exception, signal the TCS with the exception
webClientDownloadCompletionSource.SetException(e.Error);
}
else
{
// Success, write the response
currentHttpContext.Response.ContentType = "text/xml";
currentHttpContext.Response.OutputStream.Write(e.Result, 0, e.Result.Length);
// Signal the TCS that were done (we don't actually look at the bool result, but it's needed)
taskCompletionSource.SetResult(true);
}
};
string url = "url_web_service_url";
// Kick off the download immediately
client.DownloadDataAsync(new Uri(url));
// Get the TCS's task so that we can append some continuations
Task webClientDownloadTask = webClientDownloadCompletionSource.Task;
// Always dispose of the client once the work is completed
webClientDownloadTask.ContinueWith(
_ =>
{
client.Dispose();
},
TaskContinuationOptions.ExecuteSynchronously);
// If there was a callback passed in, we need to invoke it after the download work has completed
if(cb != null)
{
webClientDownloadTask.ContinueWith(
webClientDownloadAntecedent =>
{
cb(webClientDownloadAntecedent);
},
TaskContinuationOptions.ExecuteSynchronously);
}
// Return the TCS's Task as the IAsyncResult
return webClientDownloadTask;
}
public void EndProcessRequest(IAsyncResult result)
{
// Unwrap the task and wait on it which will propagate any exceptions that might have occurred
((Task)result).Wait();
}
public bool IsReusable
{
get
{
return true; // why not return true here? you have no state, it's easily reusable!
}
}
public void ProcessRequest(HttpContext context)
{
}
}发布于 2016-01-26 01:47:37
看看这个,看看它是用来干什么的。最后还有一个例子。
在许多情况下,启用任务来表示外部异步操作非常有用。TaskCompletionSource{TResult}就是为此目的而提供的。它允许创建一个可以分发给消费者的任务,这些使用者可以像其他人一样使用任务的成员。但是,与大多数任务不同,TaskCompletionSource创建的任务的状态由TaskCompletionSource上的方法显式控制。这使得外部异步操作的完成能够传播到基础任务。这种分离还确保了在不访问相应的TaskCompletionSource的情况下,使用者无法转换状态。
https://stackoverflow.com/questions/35005685
复制相似问题