1

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

在此处输入图像描述

更新:

正如 Timur 所建议的那样,我做了以下事情,但没有奏效。

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

2 回答 2

1

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

您将寻找 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. 按照此答案中的说明更新您的应用程序清单

UPD2显然你可以像这样与 WFF 控制器接口

于 2019-12-17T06:50:50.827 回答
1

这是配置管理器生成的示例。它通过更改 Web 场集合中服务器项的 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;
    }
}
于 2019-12-18T05:57:53.860 回答