如何使服务器脱机/以编程方式使服务器联机IIS Web场服务器

问题描述 投票:0回答:2

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

enter image description here

UPDATE:

按照帖木儿的建议,我做了以下操作,但是没有用。

 ServiceController p = new ServiceController("W3SVC","SERVER_IP");
    p.Start();
    p.WaitForStatus(ServiceControllerStatus.Running);
c# iis windows-server-2012-r2 web-farm-framework
2个回答
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;
    }
}

1
投票

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); }
记住,您需要以具有足够权限的用户身份运行此服务,以成功更改服务状态:就像您需要以Admin身份运行该用户一样。您至少有两个选择可以做到:

  1. 以管理员身份运行IDE

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

显然您可以与WFF控制器like so]进行接口>
© www.soinside.com 2019 - 2024. All rights reserved.