views:

264

answers:

3

I need to monitor the status of an application in the applications pool of IIS 7 from an other machine on the same domain. My monitoring application must be in C# and running as a Windows service.

On my server, I create a user with administration rights and I execute the command aspnet_regiis -ga machine\username wich worked succesfully.

My problem is when I try to access the application pool i still get COMExcepttion "Access denied". What did i do wrong or wich step did i miss?

I used code from http://patelshailesh.com/index.php/create-a-website-application-pool-programmatically-using-csharp as example.

        int status = 0;
        string ipAddress = "10.20.2.13";
        string username = "username";
        string password = "password";
        try
        {
            DirectoryEntry de = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools/MyAppPoolName", ipAddress), username, password);

            //the exception is thron here.
            status = (int)de.InvokeGet("AppPoolState");

            switch (status)
            {
                case 2:
                    //Runnig
                    break;
                case 4:
                    //Stopped
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {

        }
+1  A: 

The code you found seems to be for IIS6. Perhaps you will be better off using the new and supported IIS7 management API. You could start by calling ServerManager.OpenRemote to get the ServerManager object.

driis
Thanks for the hint, I'll take a look at it. I'll give you feedback
jack
Yes (with DCOM) : http://stackoverflow.com/questions/2544640/microsoft-web-administration-on-windows-xp/2744440#2744440
JoeBilly
A: 

You might need to mess around with the AuthenticationType, the default starting with 2.0 is Secure but you might need to set SSL. Also, I've seen Access Denied messages from accounts with the "user must change password on next logon" checked.

Chris Haas
A: 

This works pretty well on Windows 7 and Windows server 2008 (unfortunately not on XP and 2003 Server). I had to add Management Service role in the IIS via the Server Manager in order to enable remote connection.

Here's an short example of how to get the State of an Application Pool.

public ObjectState State
    {
        get
        {
            ServerManager server = null;
            ObjectState result = ObjectState.Unknown;
            try
            {
                server = ServerManager.OpenRemote(address);
                result = server.ApplicationPools[name].State;
            }
            finally
            {
                if (server != null)
                    server.Dispose();
            }

            return result;
        }
    }

Thanks to driis.

jack