Solve the Problem of background timer failure caused by IIS Automatic Recovery

Source: Internet
Author: User

Problem description: A program was developed to regularly send emails a few days ago. In actual use, it was found that the email was not sent after the scheduled completion time. The mail is sent by the background thread of Asp. Net. The specific method is to send mails one by one at a specified interval through a global Timer. Because the number of emails to be sent is large and the number of mails sent per hour is limited to 100, the entire sending process takes about 20 hours.

Solution Process: After careful detection of the program, it is found that the program itself is not a problem. As a result, event tracking is added. From the Event Viewer in Windows, we can see that Asp. Net applications will be closed from time to time. The interval is about 1 ~ Once every 5 hours. This is suspected to be a problem with IIS. After searching for related resources on the Internet, it is found that IIS will automatically Recycle resources for applications that it deems dormant to optimize server performance, recycling Resources will cause the website application to close.
This conclusion can be verified through the following methods:
1. First, add an event record to the Application_Start event of the Global. asax file to indicate the startup of the application, as shown below:
System. Diagnostics. EventLog. WriteEntry ("Beckman", "application started. ",

System. Diagnostics. EventLogEntryType. Information );
Add an application closing event record to the Application_End event:
System. Diagnostics. EventLog. WriteEntry ("Beckman", "the application is closed. ",

System. Diagnostics. EventLogEntryType. Information );
2. Open IIS, select the application assembly where the Web application is located, right-click and click "recycle"




3. Open "Event Viewer" in Windows system management tools. You can see

 

 

4. Find the problem and the solution is simple. After the first Http request for the Web application is sent, IIS will automatically start the Web application, after the Web application is closed, we can submit a request to the Web application to enable the closed application. The procedure is as follows:

 

 

In the Application_End event, after the Web application is closed for five seconds, the program generates an Http for the Web application, and IIS starts the Web application again.

Note and Description: in fact, the best solution to this problem is not based on ASP. but write another Windows service to execute such a long background task, but under some conditions, for example, the customer rents a virtual host, therefore, the solution for using Windows Services is obviously not feasible. Generally, virtual host providers do not allow installation of Windows Services, or charge fees separately. The method described in this article is still a good solution.

From orain's column

Related Article

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.