首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WebService内按时间调用IHttpAsyncHandler

在WebService内按时间调用IHttpAsyncHandler
EN

Stack Overflow用户
提问于 2016-01-26 01:38:47
回答 1查看 58关注 0票数 0

我试图在我的IHttpAsyncHandler中调用一个IHttpAsyncHandler,我发现有一个类似于使用IHttpAsyncHandler异步调用WebService的答案

我对答案有疑问。如果有人能帮我我很感激。

它有

代码语言:javascript
复制
Task webClientDownloadTask = webClientDownloadCompletionSource.Task;

我的问题是

  1. webClientDownloadCompletionSource与Webclient (client)对象无关,那么这样做有什么意义: //获取TCS的任务,以便我们可以附加一些延续任务webClientDownloadTask = webClientDownloadCompletionSource.Task;
  2. 这里的"taskCompletionSource“是什么: //向已完成的TCS发送信号(我们实际上不查看bool结果,但它是必需的)taskCompletionSource.SetResult(真);
  3. 为什么我在ContinueWith()回调中释放()ContinueWith(),为什么不在我们设置taskCompletionSource.SetResult(True)之后直接释放() WebClient呢? //总是在工作完成后释放客户机(_ TaskContinuationOptions.ExecuteSynchronously);{ client.Dispose();} )

以下是完整的代码:

代码语言:javascript
复制
    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)
    {
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-26 01:47:37

看看这个,看看它是用来干什么的。最后还有一个例子。

TaskCompletionSource

在许多情况下,启用任务来表示外部异步操作非常有用。TaskCompletionSource{TResult}就是为此目的而提供的。它允许创建一个可以分发给消费者的任务,这些使用者可以像其他人一样使用任务的成员。但是,与大多数任务不同,TaskCompletionSource创建的任务的状态由TaskCompletionSource上的方法显式控制。这使得外部异步操作的完成能够传播到基础任务。这种分离还确保了在不访问相应的TaskCompletionSource的情况下,使用者无法转换状态。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35005685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档