English 中文(简体)
如何设定 IHtpAsyncHandler 超时?
原标题:How to set IHttpAsyncHandler a timeout?

我试图在 Web. config 文件中设置执行时间 :

<compilation debug="false" targetFramework="4.5">
<httpRuntime executionTimeout="30"/>

Looking at the IIS Manager Requests page I can see the requests are not being terminated after 30 seconds.
Should I implement a Timer inside my IHttpAsyncHandler?

最佳回答

似乎缺乏对 IHttpAsyncHandler 超时支持的内在支持, 假设你必须管理自己的超时。 也许这是自作自受的; 毕竟你选择的是非同步模式 — MSFT认为谁在试图为您长期运行的任务设定默认超时?

我要做的是使用 < a href=> "http://msdn.microsoft.com/ en- us/library/ system.threading.threadpool.registerwait forsingleobject.aspx" rel="noreferrer">Threadpool.Register WaitForSingleObject 来管理您的投票。 我用一个代码样本来避免在永远不返回的网络服务上等待 :

private const int REQUEST_TIMEOUT = 30000;   // miliseconds (30 sec)
private void CallService()
        {
        try {
             string url = "somewebservice.com";
             WebRequest request = WebRequest.Create(url);

             // Asynchronously fire off the request
             IAsyncResult result = request.BeginGetResponse(new AsyncCallback(MyRoutineThatUsesTheResults), request);

             // Handle timed-out requests
             ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(RequestTimeout), request, REQUEST_TIMEOUT, true);
         }
         catch (Exception ex) {
              _logger.Error("Error during web service request.", ex);
         }

private void RequestTimeout(object state, bool timedOut)
        {
            if (timedOut) {
                WebRequest request = (WebRequest)state;
                _logger.WarnFormat("Request to {0} timed out (> {1} sec)", request.RequestUri.ToString(), REQUEST_TIMEOUT / 1000);
                request.Abort();
            }
        }

您需要使用 IAsyncResult 来使用这个方法, 但是这个模式已经确定, 您不应该有麻烦 运行样本 。

此外,当IIS决定回收您的应用程序池/ 撕掉您的应用程序域时, 您还会遇到问题。 如果您的投票仍然在运行中, 您可以使用 < a href=" http://haacked.com/ archive/2011/10/16/ the-dangers- complication- recent- black- ground- tasks- in- asp- net.aspx" rel= “ noreferr” >hosting Environment. RegisterObject 。

问题回答

您可以尝试将此添加到您的 web.config 文件 :

<system.web>
  <pages asyncTimeout="30" />
</system.web>

用于 PageAsyncTask , 但也可以为 IHttpAsyncHandler 感到荣幸吗?

If not, you may have more luck with the new HttpTaskAsyncHandler in ASP.Net 4.5 version: http://www.asp.net/vnext/overview/whitepapers/whats-new#_Toc318097378





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签