Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I unable to get Site.State for an FTP site, when using Microsoft.Web.Administration?

Tags:

c#

iis

Using the following C# code:

using System;
using Microsoft.Web.Administration;

namespace getftpstate
{
  class Program
  {
    static void Main(string[] args)
    {
      ServerManager manager = new ServerManager();
      foreach (Site site in manager.Sites)
      {
        Console.WriteLine("name: " + site.Name);
        Console.WriteLine("state: " + site.State);
        Console.WriteLine("----");
      }
    }
  }
}

I get the following output:

C:\projects\testiisftp\getftpstate\getftpstate\bin\Debug>getftpstate.exe
name: Default Web Site
state: Stopped
----
name: Default FTP Site

Unhandled Exception: System.Runtime.InteropServices.COMException (0x800710D8): T
he object identifier does not represent a valid object. (Exception from HRESULT:
 0x800710D8)
   at Microsoft.Web.Administration.Interop.IAppHostProperty.get_Value()
   at Microsoft.Web.Administration.ConfigurationElement.GetPropertyValue(IAppHos
tProperty property)
   at Microsoft.Web.Administration.Site.get_State()
   at getftpstate.Program.Main(String[] args) in C:\projects\testiisftp\getftpst
ate\getftpstate\Program.cs:line 17

Any ideas why I might be seeing the above 0x800710D8 COM error? I'm able to manage the FTP site just fine using IIS manager (I can start, stop, change settings, etc).

like image 203
Nick Bolton Avatar asked Jun 09 '10 10:06

Nick Bolton


2 Answers

The first thing to clarify is that Site.State is a property to get the state of the HTTP protocol and for that reason my guess is that the site that is throwing that exception is probably NOT an HTTP site.

If you want to get the FTP state you need to do is:

int ftpState = Site.GetChildElement("ftpServer")["state"]

You can verify if a site is HTTP or not by inspecting the Site.Bindings and looking for the Protocol property, if it does not have HTTP or HTTPS you will get the exception above which you could safely ignore.

For more information: IIS.net FTP Settings / Sample Code

like image 163
Carlos Aguilar Mares Avatar answered Sep 28 '22 10:09

Carlos Aguilar Mares


I think the answer is that this is just a bug in the Microsoft IIS API that we have to live with. I've opened a bug report on MS connect which has had 2 upvotes, however MS have shown no interest in this (and I doubt they will any time soon).

I don't believe there is a workaround for getting the actual state (as the question asks). Some have suggested that I should probe port 21, but this only tells me if there is an FTP server running, not what FTP server is running (as you can have multiple sites) -- so in some cases, this approach is completely useless.

The workaround to stopping and starting the site (which also causes a similar error) is to set auto-start to false on the FTP site and restart IIS (it's not great, but it works just fine).

like image 21
Nick Bolton Avatar answered Sep 28 '22 11:09

Nick Bolton