如何设置 IHttpAsyncHandler?
我正在尝试设置一个异步 HttpHandler,但我不知道我是否走在正确的轨道上。似乎没有太多关于它的文档。我只想收到触发 DoWork() 异步方法的请求。
有几点我不确定:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
public class WebServiceHandlerAsync : IHttpAsyncHandler
{
/// <summary>
/// BeingProcessRequest
/// </summary>
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
context.Response.Output.WriteLine("Starting");
context.Response.Flush();
// Invokes the BeginProcessRequest method on the asynchronous HTTP handler
RequestResult result = new RequestResult(context, null, extraData);
result.DoWork();
return result;
}
/// <summary>
/// Do on end of request (blocking)
/// </summary>
public void EndProcessRequest(IAsyncResult result)
{
}
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Async object
/// </summary>
public class RequestResult : IAsyncResult
{
public HttpContext Context { get; private set; }
private bool _completed;
private object _extraData;
public object AsyncState
{
get { return _extraData; }
}
public System.Threading.WaitHandle AsyncWaitHandle
{
get { throw new NotImplementedException(); }
}
public bool CompletedSynchronously
{
get { return false; }
}
public bool IsCompleted
{
get { return _completed; }
}
/// <summary>
/// Constructor
/// </summary>
public RequestResult(HttpContext context, AsyncCallback callback, object extraData)
{
Context = context;
_extraData = extraData;
}
/// <summary>
/// Handle request
/// </summary>
public void DoWork()
{
// do some work
Context.Response.Output.WriteLine("Working...");
_completed = true;
}
}
I am trying to setup an asynchronous HttpHandler, but I have no idea if I am on the right track. There doesn't seem to be much documentation on it. I would just like to get the request to fire off the DoWork() method async.
A few things I am not sure about:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
public class WebServiceHandlerAsync : IHttpAsyncHandler
{
/// <summary>
/// BeingProcessRequest
/// </summary>
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
context.Response.Output.WriteLine("Starting");
context.Response.Flush();
// Invokes the BeginProcessRequest method on the asynchronous HTTP handler
RequestResult result = new RequestResult(context, null, extraData);
result.DoWork();
return result;
}
/// <summary>
/// Do on end of request (blocking)
/// </summary>
public void EndProcessRequest(IAsyncResult result)
{
}
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Async object
/// </summary>
public class RequestResult : IAsyncResult
{
public HttpContext Context { get; private set; }
private bool _completed;
private object _extraData;
public object AsyncState
{
get { return _extraData; }
}
public System.Threading.WaitHandle AsyncWaitHandle
{
get { throw new NotImplementedException(); }
}
public bool CompletedSynchronously
{
get { return false; }
}
public bool IsCompleted
{
get { return _completed; }
}
/// <summary>
/// Constructor
/// </summary>
public RequestResult(HttpContext context, AsyncCallback callback, object extraData)
{
Context = context;
_extraData = extraData;
}
/// <summary>
/// Handle request
/// </summary>
public void DoWork()
{
// do some work
Context.Response.Output.WriteLine("Working...");
_completed = true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
去 dnrtv 观看节目 188
http://www.dnrtv.com/default.aspx?showNum=188
这将引导您了解 .NET 4 中最先进的异步以及下一个 .NET 版本中即将推出的功能。
您还可以选择在已获得商业用途许可的异步 ctp 上执行此操作。
这使得异步变得非常容易使用。
Go and watch show 188 on dnrtv
http://www.dnrtv.com/default.aspx?showNum=188
This will walk you through state of the art asynchrony within .NET 4 and the upcoming features in the next .NET release.
You will also have the option of doing this on the Asynch ctp, which is licensed for commercial use.
This makes asynchrony a blast to work with.