【问题标题】:Ho to take server offline / bring server online IIS web farm server programmatically何以编程方式使服务器脱机/使服务器联机 IIS Web 场服务器
【发布时间】:2019-12-22 19:00:11
【问题描述】:

我正在开发一个 C# 应用程序来自动化将网站部署到服务器的过程。该网站托管在 WINDOWS SERVER 2012 R2 的网络场中。所以这里的问题是我试图通过一些编程接口使服务器脱机或使其联机。但我在 Microsoft 文档中找不到任何相关内容。如何完成工作?

更新:

按照 Timur 的建议,我做了以下操作,但没有奏效。

 ServiceController p = new ServiceController("W3SVC","SERVER_IP");
    p.Start();
    p.WaitForStatus(ServiceControllerStatus.Running);

【问题讨论】:

  • 如果您只想要一个服务窗口而不是关闭整个 IIS:在应用程序文件夹中放置一个名为(完全正确!)“app_offline.htm”的文件。只要它存在,它将被交付而不是实际的应用程序。理想情况下,它将包含某种“我们正在维护,稍后再回来”屏幕。另见:Taking Web Applications Offline with Web Deploy

标签: c# iis windows-server-2012-r2 web-farm-framework


【解决方案1】:

这是配置管理器生成的示例。通过更改网络场集合中服务器项的 Enabled 属性,使服务器脱机/联机。

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{

    private static void Main()
    {

        using (ServerManager serverManager = new ServerManager())
        {
            Configuration config = serverManager.GetApplicationHostConfiguration();

            ConfigurationSection webFarmsSection = config.GetSection("webFarms");

            ConfigurationElementCollection webFarmsCollection = webFarmsSection.GetCollection();

            ConfigurationElement webFarmElement = FindElement(webFarmsCollection, "webFarm", "name", @"123213");
            if (webFarmElement == null) throw new InvalidOperationException("Element not found!");


            ConfigurationElementCollection webFarmCollection = webFarmElement.GetCollection();

            ConfigurationElement serverElement = FindElement(webFarmCollection, "server", "address", @"11.1.1.1");
            if (serverElement == null) throw new InvalidOperationException("Element not found!");

            serverElement["enabled"] = false;

            serverManager.CommitChanges();
        }
    }

    private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
    {
        foreach (ConfigurationElement element in collection)
        {
            if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
            {
                bool matches = true;

                for (int i = 0; i < keyValues.Length; i += 2)
                {
                    object o = element.GetAttributeValue(keyValues[i]);
                    string value = null;
                    if (o != null)
                    {
                        value = o.ToString();
                    }

                    if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
                    {
                        matches = false;
                        break;
                    }
                }
                if (matches)
                {
                    return element;
                }
            }
        }
        return null;
    }
}

【讨论】:

  • 恐怕这不是最好的解决方案。但它有帮助。
【解决方案2】:

IIS 是一项 Windows 服务。因此,启动/停止它的最简单方法是按照SO answer 的方式做一些事情。

您将成为looking for service name,这可能取决于您的版本。

UPD查看艺术家对您的管理工具外观的印象

var hostNames = new List<string> { "appServer1", "webServer1", "webServer2" };
foreach (var host in hostNames)
{
    var svc = new ServiceController("W3SVC", host);
    svc.Stop();
    svc.WaitForStatus(ServiceControllerStatus.Stopped);
    Thread.Sleep(10000);// or your custom logic
    svc.Start();
    svc.WaitForStatus(ServiceControllerStatus.Running);
}

请记住,您需要以具有足够权限的用户身份运行它才能成功更改服务状态:因为您需要以管理员身份运行它。 你至少有两种选择:

  1. 以管理员身份运行 IDE

  2. 按照in this answer 所述更新您的应用程序清单

UPD2 显然您可以与 WFF 控制器 like so

进行交互

【讨论】:

  • 感谢@timur 的回复。你的意思是有对应于我的四个场服务器在后台运行的 Windows 服务,我必须找到主题并启动/停止主题?
  • 回答你的问题,是的,windows服务可以远程管理。鉴于您知道场服务器的主机名,您应该能够使用上面的代码发出停止/启动命令。
  • 我应该把服务器 ip 放在 "{ "appServer1", "webServer1", "webServer2" }" 吗?
  • 我不确定 IP 地址是否有效。试一试应该不会有坏处吧?希望你有一个测试台?
  • 是的,我将 ip 作为第二个参数进行了测试,它能够构造 ServiceController 实例,但是一旦调用停止它就会抛出异常。
猜你喜欢
  • 2014-08-01
  • 2021-06-20
  • 2011-03-31
  • 2013-10-08
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-22
相关资源
最近更新 更多