Using C# to administer IIS

2

I would like to create an app that will deploy a website. Basically, I just want this app to copy the publish files to the webserver in a new folder, change the website to that folder, then recycle the app pool.

I found out that I should be able to do this with Microsoft.Web.Administration. I am having a problem with even the most basic task as I'm learning how to do this. here is my code in a windows form app to just play with this.

        try
        {
            using (Microsoft.Web.Administration.ServerManager sm = Microsoft.Web.Administration.ServerManager.OpenRemote("webservername"))
            {
                foreach (var site in sm.Sites)
                {
                    MessageBox.Show(string.Format("{0}", site.Name));
                }
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());

I'm simply trying to loop through the websites and display the names as my first try. The error I get is the following:

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Web.Administration.Interop.IAppHostWritableAdminManager'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{FA7660F6-7B3F-4237-A8BF-ED0AD0DCBBD9}' failed due to the following error: Interface not registered (Exception from HRESULT: 0x80040155).

at Microsoft.Web.Administration.ConfigurationManager.CreateAdminManager[TClass,TInterface](WebConfigurationMap webConfigMap, Boolean isAdminConfig)

at Microsoft.Web.Administration.ConfigurationManager.CreateWritableAdminManager(WebConfigurationMap webConfigMap, String configPathToEdit, Boolean isAdminConfig)

at Microsoft.Web.Administration.ConfigurationManager.CreateConfiguration(WebConfigurationMap configMap, String configPathToEdit, Boolean isAdminConfig)

at Microsoft.Web.Administration.ConfigurationManager.GetConfiguration(String rawConfigurationPath, String cacheKey, Boolean isAdminConfig)

at Microsoft.Web.Administration.ConfigurationManager.GetApplicationHostConfiguration()

at Microsoft.Web.Administration.ServerManager.GetApplicationHostConfiguration()

at Microsoft.Web.Administration.ServerManager.get_SitesSection()

at Microsoft.Web.Administration.ServerManager.get_Sites()

The line that errored is:

foreach (var site in sm.Sites)

I've tried this with both my local IIS8 express and a windows server 2008 IIS 7.5

What have I done wrong?

c#
visual-studio-2013
deployment
iis-7.5
asked on Stack Overflow Jan 21, 2015 by hondaman2003 • edited Jun 18, 2015 by Lex Li

3 Answers

1

You may want to check the following points:

  1. The Name of the server. If not sure try looking for in your IIS Management
  2. The version of the .dll is the right for your version of IIS.
  3. The .net framework version. Since it use System.Web it depends on full-version of the .net framework but not on the Client Profile subset.
answered on Stack Overflow Jan 21, 2015 by Jefry Sastre
0

You are trying to manage a remote server, so

  1. The account runs the code must be the local administrator of that remote server.
  2. The DCOM ports must be opened at firewall.
answered on Stack Overflow Jun 18, 2015 by Lex Li
0

The machine on which the code is executing needs to have the IIS Management Scripts and Tools Windows feature installed.

On a Windows client OS or on Windows Server 2008 R2 and earlier, you can install it via the following steps:

  1. Open Control Panel > Programs > Turn Windows features on or off.
  2. Drill down to Internet Information Services > Web Management Tools and check IIS Management Scripts and Tools.
answered on Stack Overflow Apr 27, 2020 by Holistic Developer

User contributions licensed under CC BY-SA 3.0