Programmatically creating a remote site in IIS7 using Microsoft.Web.Administration: Invalid index. (Exception from HRESULT: 0x80070585)

3

I'm trying to write a provisioning tool for our web application. I am attempting to create a web site remotely in IIS7.5 using Microsoft.Web.Administration.ServerManager.

CODE:

using (ServerManager serverManager = ServerManager.OpenRemote("MyRemoteHost")) {
    serverManager.Sites.Add("Contoso", "http", "*:80:www.contoso.com", @"C:\inetpub\wwwroot\Contoso");
    serverManager.CommitChanges();
}

The above code continuously fails with error: Invalid index. (Exception from HRESULT: 0x80070585)

NOTES:

  • If I run this code locally on the host, I do not encounter the error and the web site is created successfully.
  • My user has administrative privileges on the remote host.
  • I am able to perform other operations on the remote host such as reading the listing of sites.
  • My workstation is running Windows 8
  • I've managed to successfully execute the above mentioned code against a Server 2012 host. There appears to be some sort of incompatibility when running this code on a Windows 8 client against a Server 2008R2 host.
  • I'm using Microsoft.Web.Administration.7.0.0.0.

Any help in resolving this issue would be greatly appreciated.

Thank you

c#
iis
iis-7.5
servermanager
asked on Stack Overflow Oct 24, 2013 by Jean Lord • edited Nov 1, 2013 by Jean Lord

1 Answer

1

I was having the same issue too by using a remote serverManager object connected to my target server on a very large, heavily regulated corporate network.

After some investigation however, it turned out that the website WAS being created on that specific instance of server manager and, despite the error comming back, using the .CommitChanges(); method on that serverManager object would succesfully save the new website.

var serverManager = ServerManager.OpenRemote("ServerName");
var sites = string.Join(";", serverManager.Sites.Select(o => o.ToString()));
Console.WriteLine(sites);
try
{
    serverManager.Sites.Add("newWebSite", @"D:\TestSite", 80);
}
catch ( Exception e )
{
    Console.WriteLine(e);
}
Console.WriteLine(string.Join(";", serverManager.Sites.Select(o => o.ToString())));
Console.WriteLine(string.Join(";", ServerManager.OpenRemote("ServerName").Sites.Select(o => o.ToString())));
serverManager.CommitChanges();
Console.WriteLine(string.Join(";", ServerManager.OpenRemote("ServerName").Sites.Select(o => o.ToString())));
Console.ReadLine();

Using this sample code, the catch was not hit, indicating that the program still worked and I can clearly see the website added to the remote server in both the original instance of the serverManager and the new one I created.

The Console returned this:

CopyOfLive;Default Web Site
CopyOfLive;Default Web Site;newWebSite
CopyOfLive;Default Web Site
CopyOfLive;Default Web Site;newWebSite

Furthermore, checking IIS on the server proved that the website had been created.

I hope this helps!

answered on Stack Overflow Mar 7, 2016 by Alex23

User contributions licensed under CC BY-SA 3.0