问题描述
我正在尝试编写一种可以在远程服务器上向IIS添加绑定的服务.我正在使用Microsoft.Web.Administration.我添加绑定的代码如下:
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);
}
}
问题在于,在远程服务器上运行时,此代码(在本地可以正常运行)炸毁并显示Object reference not set to an instance of an object.
错误.在这种情况下,远程服务器是Windows Server 2012上的IIS 8.5.我尝试了所有可以找到的解决方案,而没有任何运气.我正在以管理员身份运行的VS 2017中运行此代码.这是我尝试过的:
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:
- 验证服务器上已安装IIS管理工具
- 验证已配置DCOM并在ahaadmin中添加了特定端点
- 关闭服务器上的防火墙.
- 关闭UAC
- 验证我具有正确版本的Microsoft.Web.Administration DLL
- 我尝试输入IP地址,服务器上配置的远程路径以及服务器名称.
由于IIS的版本不同,因此无法在本地计算机上测试我的解决方案.最终,我将编写代码以将SSL证书添加到Windows 10中不可用的IIS功能中央证书存储"中.我确实需要"OpenRemote"才能工作,但似乎没有. Microsoft没有提供ServerManager.OpenRemote()方法的示例.我发现的示例根本不起作用(大多数参考IIS7).我开始认为OpenRemote方法从未在IIS7之上的任何版本中进行过测试.有人在针对IIS 8.5的服务器上成功使用ServerManager.OpenRemote()吗?
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?
更新:这是我在VS中在调试中看到的图片:
Update: Here is a pic of what I see in VS in debug:
推荐答案
好的,我刚刚发现了这一点.首先,我坚信OpenRemote无法正常工作.我阅读的文章显示了在配置文件中传递OpenRemote的示例-这是不正确的.在MS代码中深入研究后,我看到有一个接受applicationHost.config路径的构造函数,但是没有一个接受配置路径的OpenRemote版本.这可能在IIS 7和MWA的早期版本中起作用,但肯定不能在该版本中起作用.当然,传入服务器或IP也不起作用.我将实例化ServerManager的行更改为:
OK I just figured this out. First, I am convinced that OpenRemote DOES not work. The article I read showed examples of OpenRemote passing in the config file--which is not right. After digging around a bit in MS code I see that there is a constructor that accepts the applicationHost.config path, but not a version of OpenRemote that accepts a config path. It's possible that this may have worked in IIS 7 and an earlier version of MWA but certainly not this version. Of course, passing in Server or IP doesn't work either. I changed the line that instantiates the ServerManager to:
using (ServerManager mgr = new ServerManager(@"\\qasql01\IISSharedConfig\applicationHost.config"))
,现在可以使用了.这似乎是配置远程服务器的唯一方法.至少通过这种方式,您可以使用纯ole文件安全性并绕过所有DCOM神秘的安全性要求.
and now it works. This seems to be the only way to configure a remote server. At least this way allows you to use plain ole file security and bypass all the DCOM mystical security requirements.
这篇关于.Net Core和Microsoft.Web.Administration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!