Because the website uses a large number of caches, sometimes cache errors may occur, resulting in Program Exception. In this case, you need to reclaim an application pool.
In the past, I used to connect to the server through the remote desktop, and then right-click the application pool to recycle it. This is troublesome. I found the following method to implement it.
It is actually a common Asp.net page, and a button is added for recycling. The main program is as follows:CopySave
Protected VoidStartstoprecycleapp (StringMethod ){StringApppoolname =This. Tbappname. Text. Trim ();// String method = "recycle ";Try{Directoryentry apppool =NewDirectoryentry ("IIS:// Localhost/w3svc/apppools ");Directoryentry findpool = apppool. Children. Find (apppoolname,"Iisapplicationpool"); Findpool. Invoke (method,Null); Apppool. commitchanges (); apppool. Close (); lbmsg. Text =String. Format ("Application pool {0} {1} succeeded", Apppoolname, method );}Catch(Exception ex) {lbmsg. Text =String. Format ("Application pool {0} {2} failed: {1 }", Apppoolname, Ex. Message, method );}}
Tbappname is a textbox used to enter the name of the application pool, for example, defaappapppool ".
When method = "recycle", it is recycled. If it is set to "start", it is started. If it is set to "stop", it is stopped.
Note:
1. system must be introduced. directoryservices package
2. the user must have a high permission for the application running this program. You can independently provide the application program for this program, or create a virtual directory to simulate advanced users (such as administrators or systems) in the preparation. Otherwise, the application will throw an "Access Denied" exception.