I am trying to write a service that can add bindings to IIS on a remote srver. I am using Microsoft.Web.Administration. My code to add the bindings looks like this:
public static bool AddSiteBinding(string siteName, string ipAddress, string tcpPort, string hostHeader, string protocol)
{
try
{
if (string.IsNullOrEmpty(siteName))
{
throw new ArgumentNullException("siteName", "AddSiteBinding: siteName is null or empty.");
}
//get the server manager instance
using (ServerManager mgr = ServerManager.OpenRemote(@"\\Qasql01\c$\Windows\System32\inetsrv\config\applicationHost.config"))
//using (ServerManager mgr = new ServerManager())
{
SiteCollection sites = mgr.Sites;
Site site = mgr.Sites[siteName];
if (site != null)
{
string bind = ipAddress + ":" + tcpPort + ":" + hostHeader;
//check the binding exists or not
foreach (Binding b in site.Bindings)
{
if (b.Protocol == protocol && b.BindingInformation == bind)
{
throw new Exception("A binding with the same ip, port and host header already exists.");
}
}
Binding newBinding = site.Bindings.CreateElement();
newBinding.Protocol = protocol;
newBinding.BindingInformation = bind;
site.Bindings.Add(newBinding);
mgr.CommitChanges();
return true;
}
else
throw new Exception("Site: " + siteName + " does not exist.");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
The problem is that this code (which works fine locally) blows up with an Object reference not set to an instance of an object. error when it's run against the remote server. The remote server in this case is IIS 8.5 on Windows Server 2012. I have tried every solution I can find without any luck at all. I am running this code from VS 2017 which is running as an administrator. Here's what I've tried:
- Verified that the IIS management tools are installed on the server
- Verified that DCOM is configured and added a specific endpoint in ahaadmin
- Turned the firewall on the server off.
- Turned off UAC
- Verified that I have the right version of Microsoft.Web.Administration DLL
- I have tried entering the IP address, the remote path to the config on the server, and the server name.
I cannot test my solution on my local machine because the versions of IIS are different. Ultimately I will be writing code to add an SSL certificate to the "Central Certificate Store" which is an IIS feature not available in Windows 10. I really need "OpenRemote" to work, but it seems like it doesn't. There are no examples from Microsoft for the ServerManager.OpenRemote() method. The examples that I have found simply don't work (most reference IIS7). I'm beginning to think the OpenRemote method was never tested in anything above IIS7. Is anyone out there successfully using ServerManager.OpenRemote() against IIS 8.5?
