Asp. NET Background long-running tasks

Source: Internet
Author: User

First, it is not recommended to start the long-running task in ASP. Because neither the task nor the threadpool.queueuserworkitem,asp.net will know that they are running in the background, which can cause problems such as:

When you modify the Web. config, the AppDomain is triggered to be recycled (although the IIS Web server process w3wp.exe is still alive), IIS itself recycles the application pool every 29 hours, which causes the background thread to be terminated, which throws an exception.

When ASP. NET to reclaim the AppDomain, it causes the existing requests to be processed and recycled, and the ASP. NET and IIS servers also know that the requests are running, so they are done. The problem is that ASP. NET does not know any background thread such as a timer or anything else, it only knows the operation associated with the request.

In fact, running certain tasks in the background for a long time is not something that Web server does, and usually avoids doing so in other ways, such as:

With the console application and Windows Task Manager, or using Windows services.

However, if you decide to do this, there are some ways to ensure that the background task exits safely in ASP.

Method One, use the IRegisteredObject interface.

The IRegisteredObject interface is passed and the Hostingenvironment.registerobject method is called to register it in ASP.

When the AppDomain is being recycled, the Stop method in the IRegisteredObject in the registered object is called.

public interface IRegisteredObject {void Stop (bool immediate);

The registered object is not unregistered (that is, the Hostingenvironment.unregisterobject method is not called to unregister), then the Stop method is called two times, the first time the call, the immediate parameter is false, if the registered object has stopped, then You should call the Hostingenvironment.unregisterobject method to cancel the registration. If the registration is not unregistered, then 30 seconds later, the method will be executed again (this is the last chance), the difference is that the incoming immediate parameter is true, at this time the registered object must first call the Unregisterobject method and then return Otherwise, the application Manager will remove the registration for that object.

Using the IRegisteredObject interface is not intended to handle background long-running tasks, but this feature allows you to safely exit the background task.

For example, the following:

Public class jobhost : iregisteredobject{    private readonly  object _lock = new object ();     private bool _ Shuttingdown;    public jobhost ()     {         hostingenvironment.registerobject (This);    }     public void stop (bool immediate)     {         lock  (_lock)         {             _shuttingDown = true;         }        hostingenvironment.unregisterobject (this);     }    public void dowork (Action work)      {        lock  (_lock)         {             if  (_shuttingdown)              {                 return;            }             work ();         }    }}

As the above code, ASP. NET to reclaim the AppDomain, the Stop method is called, this method obtains a lock, and the DoWork method obtains the same, so that the Stop method has to wait while the DoWork is running.

Method two, using Hostingenvironment.queuebackgroundworkitem

Hostingenvironment.queuebackgroundworkitem method in. Introduced in NET4.5.2, Queuebackgroundworkitem (QBWI) registers a background task when it runs through the ASP. In this case, since ASP. NET knows that there is a background task, he does not immediately reclaim the AppDomain, which, of course, does not mean that the background task can do anything. When ASP. NET needs to be recycled, it can notify the background task through a cancellationtoken and wait for 30s to complete the work. If you don't ask in 30 seconds, the job will disappear. For more details on Queuebackgroundworkitem, you can

http://blogs.msdn.com/b/webdev/archive/2014/06/04/ Queuebackgroundworkitem-to-reliably-schedule-and-run-long-background-process-in-asp-net.aspx study.

Method two, using Hangfire

Hangfire is an open-source class library that provides an easy way to perform background long-running tasks in ASP. This class library requires some additional storage support, Sqlserver,redis or MSMQ. Hangfire's information is in http://hangfire.io/.

Resources

http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/

Http://blog.stephencleary.com/2014/06/fire-and-forget-on-asp-net.html

Http://blogs.msdn.com/b/tmarq/archive/2010/04/14/performing-asynchronous-work-or-tasks-in-asp-net-applications.aspx

This article is from "a blog" blog, make sure to keep this source http://cnn237111.blog.51cto.com/2359144/1549947

Asp. NET Background long-running tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.