ToDotNet

Convert.ToDotNet(InstanceOfWorld)

  Home :: Contact :: Syndication  :: Login
  174 Posts :: 0 Stories :: 175 Comments :: 22 Trackbacks

News





My Vista Score...
3.4




Archives

Post Categories

Image Galleries

Affiliations

Blogs I Read

Interesting Links

My Links

Not so long ago I needed to write a little piece of code to allow some to recycle the application from within a web application. Ok, recycling the AppPool that would be running the web application sounded a bit silly, but any other AppPool would be possible. Enter the world of WMI. Or rather, take a glimpse inside, because there's no way this post will do justice to the power of System.Management. Here's the little piece that I wrote, transformed into a console application.

   1:  static void ListApplicationPools(string hostname)
   2:  {
   3:  string ClassPath = @"\\" + hostname + @"\root\microsoftiisv2:IIsApplicationPoolSetting";
   4:  ManagementClass WmiObject = new ManagementClass(ClassPath);
   5:   
   6:  foreach(ManagementObject child in WmiObject.GetInstances())
   7:  {
   8:  char[] SplitChar = "/".ToCharArray();
   9:  Console.WriteLine(child.ToString().Split(SplitChar)[child.ToString().Split(SplitChar).Length-1].Replace(@"""",""));
  10:  }
  11:  }

The first method lists the application pools that are on the specified host. So you can run this method on the localhost, but also on a remote server, as long as you have administrative access to the specified host.

   1:  static void RunAppPoolCommand(string hostname, string command, string apppoolname)
   2:  {
   3:  string ClassPath = @"\\" + hostname + @"\root\microsoftiisv2:IIsApplicationPool='W3SVC/AppPools/" + apppoolname + "'";
   4:  ManagementObject WmiObject = new ManagementObject(ClassPath);
   5:   
   6:  InvokeMethodOptions Options = new InvokeMethodOptions();
   7:  Options.Timeout = new TimeSpan(0,0,10);
   8:   
   9:  ManagementBaseObject InParams = WmiObject.GetMethodParameters(command);
  10:  ManagementBaseObject OutParams = WmiObject.InvokeMethod(command, null, Options);
  11:   
  12:  if (InParams != null) InParams.Dispose();
  13:  if (OutParams != null) OutParams.Dispose();
  14:  }

The second method allows you to send commands to the specified Application Pool. These commands can be: stop, start and recycle.

You see, it's very easy... once you know how. You can download the compiled binary here.

 

posted on Saturday, August 06, 2005 5:20 PM



Feedback

# re: Control the Application Pool 8/9/2005 11:21 AM Clemens Reijnen
found this one: WMI Code Creator v1.0


http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&DisplayLang=en

# re: Control the Application Pool 8/9/2005 11:25 AM Sander
Wow, that's hot from the press (Aug, 5th 2005). Thanks.

# re: Control the Application Pool 8/9/2005 12:02 PM Clemens Reijnen
Yep... verry hot ;-)
working alot with wmi at the moment... for the deployment and configuration of biztalk solutions... take a look at the gotdotnet workspace: Biztalk Transactional Deployment Console Application

Post Feedback

Title:
Name:
Url:
Comments: 
Protected by Clearscreen.SharpHIPEnter the code you see: