Restart IIS application pool from ASP.NET page

I’ve been developing this .NET class library as a COM object consumable by Classic ASP. Every time I go to build my project, it would tell me:

Unable to copy file "..\Core\bin\Debug\Core.dll" to "bin\Debug\Core.dll". The process cannot access the file 'bin\Debug\Core.dll' because it is being used by another process.

It turns out that the process that had a lock on my dll file was the IIS application pool process. After about a day of using remote desktop to login to the development server, opening up IIS and stopping the application pool, pressing Alt-Tab to get back to Visual Studio and swearing because Remote Desktop hijacks my Alt-Tab, painstakingly lifting my hand to my mouse, minimizing Remote Desktop, maximizing Visual Studio and building my library, trying the new build in my browser and swearing when I see “Service Unavailable” because the application pool is stopped… you get the picture.

This code enables you to stop and start your application pool from the comfort of your own browser. It also gives you your application pool’s status by monitoring AppPoolState.

IIS Application Pool restart .aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="iis.aspx.cs" Inherits="service.iis" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>IIS App Restart</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Status: <asp:Label ID="lblStatus" runat="server" /><br/>
        <asp:Button ID="btnStop" Text="STOP App Pool" BackColor="IndianRed" ForeColor="White" runat="server" CommandArgument="dev.somesite.com" OnClick="stopAppPool" /><br />
        <asp:Button ID="btnStart" Text="START App Pool" BackColor="Lime" runat="server" CommandArgument="dev.somesite.com" OnClick="startAppPool" /><br />
    </div>
    </form>
</body>
</html>

Remember to replace “dev.somesite.com” in the CommandArgument attribute of the two buttons with the name of your application pool.

Codebehind .aspx.cs file

using System;
using System.Web;
using System.Web.UI;
using System.Management;
using System.DirectoryServices;
using System.Web.UI.WebControls;

public partial class iis : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(System.Environment.MachineName);
        status();
    }

    protected void status()
    {
        string appPoolName = "dev.somesite.com";
        string appPoolPath = @"IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/" + appPoolName;
        int intStatus = 0;
        try
        {
            DirectoryEntry w3svc = new DirectoryEntry(appPoolPath);
            intStatus = (int)w3svc.InvokeGet("AppPoolState");
            switch (intStatus)
            {
                case 2:
                    lblStatus.Text = "Running";
                    break;
                case 4:
                    lblStatus.Text = "Stopped";
                    break;
                default:
                    lblStatus.Text = "Unknown";
                    break;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
    protected void stopAppPool(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string appPoolName = btn.CommandArgument;
        string appPoolPath = @"IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/" + appPoolName;
        try
        {
            DirectoryEntry w3svc = new DirectoryEntry(appPoolPath);
            w3svc.Invoke("Stop", null);
            status();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    protected void startAppPool(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string appPoolName = btn.CommandArgument;
        string appPoolPath = @"IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/" + appPoolName;
        try
        {
            DirectoryEntry w3svc = new DirectoryEntry(appPoolPath);
            w3svc.Invoke("Start", null);
            status();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}

You should probably stick this little page on a separate site using a different application pool :)

2 Comments

  1. Nuzhat
    Posted October 27, 2008 at 10:56 am | Permalink

    With the user of code, I am getting following error.

    DEV1System.Runtime.InteropServices.COMException (0×80070005): Access is denied. at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind() at System.DirectoryServices.DirectoryEntry.get_NativeObject() at System.DirectoryServices.DirectoryEntry.InvokeGet(String propertyName) at _Default.status()

  2. John stables
    Posted September 28, 2009 at 10:43 am | Permalink

    hi I am getting the following error?
    could you advise me if there is anything i need to do differently because my machine is Vista? or how to rectify this problem i have named the ASPX file emailtest.aspx
    and the code behind iis.aspx.cs ive been having a few problems when trying to include C# into my ASP pages so wondering if it is a vista related problem?

    Parser Error
    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

    Parser Error Message: Could not load type ’service.iis’.

    Source Error:

    Line 1:
    Line 2:
    Line 3:

    Source File: /sitehelpdesk/email/emailtest.aspx Line: 1


Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*