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 🙂

29 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 (0x80070005): 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()

    • Uday
      Posted September 7, 2010 at 11:32 am | Permalink

      Try executing the exe with administrator role. Right click on the exe and click Run as administrator.

  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

  3. ddeconin
    Posted January 21, 2011 at 2:11 pm | Permalink

    Hi, I also got to the point where I get this parser error message.
    Where do I put this Codebehind.aspx.cs file ?

    • fred
      Posted June 7, 2011 at 3:59 pm | Permalink

      Did you ever figure out how to fix the parser error?

  4. Posted July 28, 2011 at 5:37 pm | Permalink

    Jp62px http://fnYwlOpd2n9t4Vx6A3lbk.com

  5. Kizhak
    Posted August 10, 2011 at 9:27 pm | Permalink

    I am getting ComException: No mapping between account names and security IDS was done …
    This when I am invoking these methods from a windows service. this however works in a windows forms application…Anything I am doing wrong.
    thanks in advance
    Kizhakk

  6. Posted September 6, 2011 at 3:22 am | Permalink

    Very funny pictures free xxx teen porn %-OOO

  7. Posted September 6, 2011 at 1:01 pm | Permalink

    Hold the line, please teen horney porn 652

  8. Posted September 6, 2011 at 8:41 pm | Permalink

    I’m on business top lolita
    8[[

  9. Posted September 7, 2011 at 10:20 pm | Permalink

    I’ll put him on pedo child gallery
    %D

  10. Posted September 19, 2011 at 8:45 am | Permalink

    Just over two years Kid Porn Pedo
    lqz

  11. Posted September 23, 2011 at 5:31 am | Permalink

    I love this site Lolita Nude
    =DDD

  12. Posted September 28, 2011 at 10:50 pm | Permalink

    I enjoy travelling top lolita pay sites 186

  13. Posted April 3, 2012 at 11:17 am | Permalink

    System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000) at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) at System.DirectoryServices.DirectoryEntry.Bind() at System.DirectoryServices.DirectoryEntry.get_NativeObject() at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args) at _Default.StopIIS_Click(Object sender, EventArgs e)

  14. Posted May 6, 2012 at 12:00 pm | Permalink

    I’m sorry, he’s http://udyfopurykuf.de.tl girl naughty preteen Asian FOB girls are like handicap when it comes to sex. LIKE FKIN a star FISH!! WOW! SO BAD OMG!

  15. Posted February 16, 2013 at 1:40 pm | Permalink

    I am curious to find out what blog system you
    happen to be utilizing? I’m experiencing some minor security problems with my latest site and I would like to find something more secure. Do you have any recommendations?

  16. Posted April 9, 2013 at 1:12 am | Permalink

    Best Site Good Work http://www.zoji.com/1230648 video collections lolitas jpg the sounds she;s making are annoying as fuck but his dick is thick as hell, with a penis like that, he would do damage, in a good way

  17. Posted April 10, 2013 at 3:45 am | Permalink

    I was born in Australia but grew up in England http://community.parents.com/asumouooi/blog/2013/04/04/lolita_kingdom_nude_pics sven bbs lolita preteen Hum… i think you’re the most gorgeous and classy girl i have ever seen in my life, and that guy must be the luckiest man on earth. Damn, that was great.

  18. Posted April 10, 2013 at 3:46 am | Permalink

    Could you give me some smaller notes? free nude lolita guestbook someone needs to put a recent vid of her cause her tits are huge, they are the same size as eva angelina!

  19. Posted April 10, 2013 at 4:03 am | Permalink

    Which university are you at? nude lolitas thumbnails child she is soooooooooo hot

  20. Posted May 3, 2013 at 12:38 am | Permalink

    EXCELLENT!!

    Very use full!!

    Thank you very much

    you save me!!

    German

  21. Posted May 30, 2013 at 6:58 am | Permalink

    Exceptional post however I was wondering if you could write
    a litte more on this subject? I’d be very grateful if you could elaborate a little bit further. Appreciate it!

  22. Posted June 22, 2013 at 11:02 am | Permalink

    For a nice and surfing on-line above 3 hours currently, having said that i certainly not discovered any kind of interesting content including your own house. It can be attractive truly worth enough personally. In my view, in case all webmasters as well as blog writers made superior information when you have, the web will certainly be a great deal more practical than ever before.

  23. Posted August 19, 2013 at 3:05 pm | Permalink

    Aw, this was an exceptionally nice post. Spending some
    time and actual effort to make a great article… but what can
    I say… I put things off a lot and don’t manage to get nearly anything done.

  24. Posted November 28, 2013 at 4:33 am | Permalink

    I’d like some euros redpornblog
    That girl is fit as fuck. I want to fuck her so hard and fast and cum on that stunning face of hers.

  25. Posted October 14, 2014 at 8:56 am | Permalink

    If you wish for to grow your experience simply
    keep visiting this site and be updated with the most up-to-date information posted here.

  26. Posted August 26, 2015 at 12:15 pm | Permalink

    On btnStop_Click Event, I am getting following error.

    System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) — End of inner exception stack trace — at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args) at restore.btnStop_Click


Post a Comment to Uday

Required fields are marked *

*
*