How to stop an Application Pool in IIS 6.0 in c#?

M

M Craig

I'm trying to write a custom installation engine to plug into our
existing build system. Some things I'm trying to do are,
Create/Delete/Start/Stop Application Pools, Web Sites, and Virtual
Directories.

At this point I'd be happy if someone can provide a sample of how to
Stop an application pool. Or point me in the right direction.

Thanks!

Mike
 
M

Michael Nemtsev

Hello M,

what's your "application pools" ?
How do u create them?

Not so clear for me

MC> I'm trying to write a custom installation engine to plug into our
MC> existing build system. Some things I'm trying to do are,
MC> Create/Delete/Start/Stop Application Pools, Web Sites, and Virtual
MC> Directories.
MC>
MC> At this point I'd be happy if someone can provide a sample of how to
MC> Stop an application pool. Or point me in the right direction.
MC>
MC> Thanks!
MC>
MC> Mike
MC>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
M

M Craig

In IIS 6.0 I manually create seperate application pools for each web
site I host on our servers. This way each web site runs in it's own
memory space and can be recycled independant of the other web sites.

Steps to Create:
1. Open compmgmt.msc
2. Expand Services and Applications
3. Expand Internet Information Services
4. Expand Application Pools (Windows 2003 Server)
5. New -> Application Pool...
6. Give it a name, click OK.
7. Open properties to a web site
8. Click Home Directory
9. Change Application pool to your new pool name. click Ok.
 
M

Michael Nemtsev

Hello M,

AFAIK, pools are created by IIS for each new virtual folder, u need only
to point that this folder works with that pool
ADSI helps u to manage IIS pools, but ADSI is unmanaged http://msdn.microsoft.com/library/d...html/396f5af7-f517-4e8b-9eb7-5a5344a7b851.asp

To stop pool use IISApplicationPool::Stop http://msdn.microsoft.com/library/d...html/91b7a69f-3819-4429-b87f-76b2de05644c.asp

in this case u need to make your calls via interop

MC> In IIS 6.0 I manually create seperate application pools for each web
MC> site I host on our servers. This way each web site runs in it's own
MC> memory space and can be recycled independant of the other web sites.
MC>
MC> Steps to Create:
MC> 1. Open compmgmt.msc
MC> 2. Expand Services and Applications
MC> 3. Expand Internet Information Services
MC> 4. Expand Application Pools (Windows 2003 Server)
MC> 5. New -> Application Pool...
MC> 6. Give it a name, click OK.
MC> 7. Open properties to a web site
MC> 8. Click Home Directory
MC> 9. Change Application pool to your new pool name. click Ok.
MC> Michael Nemtsev wrote:
MC>---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
M

M Craig

Thanks but C++ isn't my thing! :)

I did find a solution:
const string WebServerSchema = "IIsApplicationPools"; // Case Sensitive
string ServerName = "LocalHost";
DirectoryEntry W3SVC = new DirectoryEntry("IIS://" +
ServerName + "/w3svc", "", "");

foreach (DirectoryEntry Site in W3SVC.Children)
{
Console.WriteLine(Site.SchemaClassName);
if (Site.SchemaClassName == WebServerSchema)
foreach (DirectoryEntry child in Site.Children)
{
Console.WriteLine(child.Parent + ": " +
child.Name);
if (child.Name == appPoolName)
{
PropertyCollection appPoolProps =
child.Properties;
foreach (string PropName in
appPoolProps.PropertyNames)
{
Console.WriteLine("Application
Pool: {0} - {1}", PropName, appPoolProps[PropName].Value);
}
appPoolProps["AppPoolCommand"].Value =
AppPoolCommandEnum.stop; // 2==stop, 1==start
child.CommitChanges();
}
}
Console.WriteLine(Site.Name + " - " +
Site.Properties["ServerComment"].Value.ToString());
}

This is working for me.

Thanks.
 
M

M Craig

Thanks but C++ isn't my thing! :)

I did find a solution:
const string WebServerSchema = "IIsApplicationPools"; // Case Sensitive
string ServerName = "LocalHost";
DirectoryEntry W3SVC = new DirectoryEntry("IIS://" +
ServerName + "/w3svc", "", "");

foreach (DirectoryEntry Site in W3SVC.Children)
{
Console.WriteLine(Site.SchemaClassName);
if (Site.SchemaClassName == WebServerSchema)
foreach (DirectoryEntry child in Site.Children)
{
Console.WriteLine(child.Parent + ": " +
child.Name);
if (child.Name == appPoolName)
{
PropertyCollection appPoolProps =
child.Properties;
foreach (string PropName in
appPoolProps.PropertyNames)
{
Console.WriteLine("Application
Pool: {0} - {1}", PropName, appPoolProps[PropName].Value);
}
appPoolProps["AppPoolCommand"].Value =
AppPoolCommandEnum.stop; // 2==stop, 1==start
child.CommitChanges();
}
}
Console.WriteLine(Site.Name + " - " +
Site.Properties["ServerComment"].Value.ToString());
}

This is working for me.

Thanks.
 
W

Willy Denoyette [MVP]

| I'm trying to write a custom installation engine to plug into our
| existing build system. Some things I'm trying to do are,
| Create/Delete/Start/Stop Application Pools, Web Sites, and Virtual
| Directories.
|
| At this point I'd be happy if someone can provide a sample of how to
| Stop an application pool. Or point me in the right direction.
|
| Thanks!
|
| Mike


Using System.Management and WMI is one option using scripting is another.

StopAppPool("admin", "adminPwd", "remServer", "W3SVC/AppPools/MyAppPool");

....

static void StopAppPool(string ConnectionUser, string ConnectionPassword,
string Machine, string appPool )
{
ConnectionOptions co = new ConnectionOptions();
co.Username = ConnectionUser;
co.Password = ConnectionPassword;
co.Impersonation = ImpersonationLevel.Impersonate;
co.Authentication = AuthenticationLevel.PacketPrivacy;
string objPath = "IISApplicationPool.Name='" + appPool + "'"; // watch the
single quotes!!

ManagementScope scope = new ManagementScope(@"\\" + Machine +
@"\root\MicrosoftIISV2", co);

using(ManagementObject mc = new ManagementObject(objPath))
{
mc.Scope = scope;
mc.InvokeMethod("Stop", null, null);
}
}

Please consult:
http://msdn.microsoft.com/library/d...html/77e8594d-8ebe-4887-9682-4343ffc70e16.asp

for more details.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top