![](/img/trans.png)
[英].Net Core Microsoft.Web.Administration ServerManager from Linux
[英].Net Core and Microsoft.Web.Administration
我正在尝试编写一种可以在远程服务器上向IIS添加绑定的服务。 我正在使用Microsoft.Web.Administration。 我添加绑定的代码如下所示:
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中运行此代码。 这是我尝试过的:
我无法在本地计算机上测试解决方案,因为IIS的版本不同。 最终,我将编写代码以向“中央证书存储”中添加SSL证书,这是Windows 10中不提供的IIS功能。我确实需要“ OpenRemote”才能工作,但似乎并没有。 Microsoft没有提供ServerManager.OpenRemote()方法的示例。 我发现的示例根本不起作用(大多数参考IIS7)。 我开始认为OpenRemote方法从未在IIS7之上的任何版本中进行过测试。 有没有人针对IIS 8.5成功使用ServerManager.OpenRemote()?
好吧,我只是想通了。 首先,我坚信OpenRemote不起作用。 我阅读的文章显示了在配置文件中传递OpenRemote的示例-这是不正确的。 在MS代码中进行了一些研究之后,我看到有一个接受applicationHost.config路径的构造函数,但是没有一个接受配置路径的OpenRemote版本。 这可能在IIS 7和MWA的早期版本中起作用,但肯定不能在该版本中起作用。 当然,传入服务器或IP也不起作用。 我将实例化ServerManager的行更改为:
using (ServerManager mgr = new ServerManager(@"\\qasql01\IISSharedConfig\applicationHost.config"))
现在可以了。 这似乎是配置远程服务器的唯一方法。 至少通过这种方式,您可以使用纯Ole文件安全性并绕过所有DCOM神秘的安全性要求。
我可以确认user2033791的答案像一个超级按钮(IIS8.5 / 2012R2)。 远程IIS系统会自动获取额外的绑定,并且其他应用程序不会被回收。
使用[Browsable(false)]
属性隐藏使用的构造函数,根据MSDN,此构造函数仅供Microsoft内部使用。 所以买家要当心;-)
参考文献:
https://msdn.microsoft.com/zh-cn/library/microsoft.web.administration.servermanager.servermanager(v=vs.90).aspx https://referencesource.microsoft.com/#Microsoft.Web.Administration /Microsoft/Web/Administration/ServerManager.cs,d3122afbddb2cfa0
据我了解, ServerManager.OpenRemote()用于获取服务器名称,而不是applicationHost.config的路径。 也许那是问题所在?
当我升级Microsoft.Web.Administration NuGet程序包时,它停止为我工作。 当我降级回7.0.0.0版本时,效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.