.Net Core和Microsoft.Web.Administration

时间:2017-11-24 22:08:33

标签: c# .net ssl iis

我正在尝试编写一个可以在远程srver上添加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运行此代码,该代码以管理员身份运行。这是我尝试过的:

  1. 已验证IIS管理工具已安装在服务器上
  2. 已验证DCOM已配置并在ahaadmin中添加了特定端点
  3. 关闭服务器上的防火墙。
  4. 关闭UAC
  5. 已验证我拥有正确版本的Microsoft.Web.Administration DLL
  6. 我尝试输入IP地址,服务器上配置的远程路径以及服务器名称。
  7. 我无法在本地计算机上测试我的解决方案,因为IIS的版本不同。最终,我将编写代码以将SSL证书添加到“中央证书存储”,这是Windows 10中不可用的IIS功能。我真的需要“OpenRemote”才能工作,但似乎没有。 Microsoft没有ServerManager.OpenRemote()方法的示例。我发现的示例根本不起作用(大多数参考IIS7)。我开始认为OpenRemote方法从未在IIS7之上的任何东西中进行过测试。 是否有人成功使用ServerManager.OpenRemote()对IIS 8.5?

    更新:这是我在调试中在VS中看到的图片: VS Debug

3 个答案:

答案 0 :(得分:0)

好的,我只想出来了。首先,我确信OpenRemote不起作用。我读过的文章展示了OpenRemote在配置文件中传递的例子 - 这是不对的。在MS代码中挖掘了一下后,我发现有一个构造函数接受了applicationHost.config路径,但没有接受配置路径的OpenRemote版本。这可能在IIS 7和早期版本的MWA中有效,但肯定不是这个版本。当然,传递服务器或IP也不起作用。我将实例化ServerManager的行更改为:

using (ServerManager mgr = new ServerManager(@"\\qasql01\IISSharedConfig\applicationHost.config"))

现在它有效。这似乎是配置远程服务器的唯一方法。至少这种方式允许您使用普通的ole文件安全性并绕过所有DCOM神秘的安全要求。

答案 1 :(得分:0)

我可以确认user2033791的答案就像魅力(IIS8.5 / 2012R2)。远程IIS系统自动拾取额外的绑定,而其他应用程序不会被回收。

使用的构造函数隐藏了[Browsable(false)]属性,根据MSDN,仅供Microsoft内部使用。所以买家要小心;-)

参考文献:

https://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager.servermanager(v=vs.90).aspx https://referencesource.microsoft.com/#Microsoft.Web.Administration/Microsoft/Web/Administration/ServerManager.cs,d3122afbddb2cfa0

答案 2 :(得分:0)

据我了解,https://www.progress.com/documentation/sitefinity-cms/overview-page-templates是要使用服务器名称,而不是applicationHost.config的路径。也许是问题所在?

当我升级Microsoft.Web.Administration NuGet程序包时,它不再为我工作。当我降级到7.0.0.0版本时,效果很好。

相关问题