Creating Windows Services, timed monitoring Web site application Pools

Source: Internet
Author: User

The recent web site always reported "timer_connection" error, resulting in the site used by the application pool due to too many errors to stop running, the site also appeared the service Unvaliable, unable to visit, on the Internet to check a lot of information, the result is very frustrating, This problem has been bothering me for a long time, has not been resolved, and later colleagues sent an article to let me have a new solution, although not fundamentally solve this error, but can quickly empty and restore the specified application pool

Don't say more, look at this method, the main is to create a connection (such as you want to monitor the site of an address), if you can access this page to explain the site application pool is not a problem, if not access, that is, will be an error, then we can call the following method, to empty and restart the application pool, The code is as follows:

public void Restartapppool ()
{
System.Net.HttpWebRequest request = (HttpWebRequest) webrequest.create (Sys Tem. Configuration.ConfigurationSettings.AppSettings

["WebFile"].      ToString ());
Request.      KeepAlive = false;
Try
{
System.Net.HttpWebResponse response = (HttpWebResponse) request.        GetResponse ();
Response.      Close ();
catch
{
DirectoryEntry root = new DirectoryEntry ("iis://localhost/w3svc/apppools/" + System.Configuration.ConfigurationSettings.AppSettings

["AppPoolName"].        ToString ());
Root.        Invoke ("Start", null);
Root.commitchanges ();
Addeventlog ("Restart application pool in:" +datetime.now.tolongtimestring ());
Root.      Close ();
}
}

Here's one thing to note, DirectoryEntry need to refer to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.DirectoryServices.dll

Also using System.Net

Instead, I created a Windows service to check the status of the application pool regularly.

Also, when you create Windows service, do not forget to set account to local System in the setup Process Control (ServiceProcessInstaller1) property, otherwise you will be prompted when you install the service You enter username and password, the following figure

At this point you need to create a Windows account before installing the service, and you need to add the user you created before by "Log on as a service" in the policy, and of course it's cumbersome to do this, so be sure to set account to local System to avoid this problem in the installation. I have met this problem before, and finally have to search a circle on the internet to understand what is going on, I hope you encountered this problem is to help you oh.

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.