1
votes

I have created a method to get hosted web sites from IIS server as following code snippet.

       ServerManager serverManager = new ServerManager();

        try
        {
            foreach (Site site in serverManager.Sites)
            {
                Console.WriteLine(site);
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);

            Console.ReadLine();
        }

When i run it in my local machine it's working perfectly(Windows 7 /IIS 7 with 32bits).but when i run it in server machine(Windows server 2003 R2 with IIS 6) it's not working.It gives following Error

Retrieving the COM class factory for component with CLSID {2B> 52-803546CE3344} failed due to the following error: 80040154> d (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

Any help will be great full ?

2

2 Answers

2
votes

I have used System.DirectoryServices instead of using Microsoft.Web.Administration and it solved my problem.It will work with the IIS6 and IIS7 as well.

    public class IisManager
    {
        public static int IisVersion
        {
            get
            {
                int iisVersion;

                using (RegistryKey iisKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp"))
                {
                    if (iisKey == null)
                        throw new Exception("IIS is not installed.");
                    iisVersion = (int)iisKey.GetValue("MajorVersion");
                }

                return iisVersion;
            }

        }

        public static IList<IisWebSite> GetIisSites()
        {
            List<IisWebSite> sites = new List<IisWebSite>();

            using (DirectoryEntry iisRoot = new DirectoryEntry("IIS://localhost/W3SVC"))
            {
                iisRoot.RefreshCache();
                sites.AddRange(iisRoot.Children.Cast<DirectoryEntry>().Where(w => w.SchemaClassName.ToLower(CultureInfo.InvariantCulture) == "iiswebserver").Select(w => new IisWebSite(w.Name, w.Properties["ServerComment"].Value.ToString())));
            }

            return sites;
        }

        public static IList<string> GetIisAppPools()
        {
            List<string> pools = new List<string>();
            using (DirectoryEntry poolRoot = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"))
            {
                poolRoot.RefreshCache(); pools.AddRange(poolRoot.Children.Cast<DirectoryEntry>().Select(p => p.Name));
            }
            return pools;
        }

    }
1
votes

Check out this blog post, most specifically, the last paragraph. It's very possible that it's a 32bit vs 64bit DLL compilation conflict

Both the customer and I were creating 32-bit .NET applications, and the COM interface for the FTP runtime state is implemented in a 64-bit-only DLL. Once we both changed our projects to compile for 64-bit platforms, we were both able to get the code to run. (Coincidentally, all I had was a 32-bit system when I wrote my original blog, so I probably would have run into this sooner if I had owned a 64-bit system way back then. ;-])

http://blogs.iis.net/robert_mcmurray/archive/2012/06/29/error-class-not-registered-0x80040154-when-querying-ftp-runtime-state.aspx