Behnam Esmaili
Behnam Esmaili

Reputation: 5967

Ho to take server offline / bring server online IIS web farm server programmatically

I am developing a C# application to automate process of deploying website to the server.The website is hosted in a web farm in WINDOWS SERVER 2012 R2. So the problem here is I am trying to take server offline or bring it online by means of some programming interface. but I couldn't find anything related inside Microsoft docs. How do I get the job done?

enter image description here

UPDATE:

As suggested by Timur I did as following, but it didn't work.

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

Upvotes: 1

Views: 1130

Answers (2)

Jokies Ding
Jokies Ding

Reputation: 3494

This is the sample that generated by configuration manager. It take server offline/online by change the Enabled property of server item in web farm collection.

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;
    }
}

Upvotes: 1

timur
timur

Reputation: 14577

IIS is a Windows service. Therefore the easiest way to start/stop it will be to do something along the lines of this SO answer.

You'll be looking for service name, which likely depends on your version.

UPD see an artist's impression on how your management tool might look like

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);
}

bear in mind, you'll need to run this as a user with sufficient privileges to successfully change service state: as in you need to run this as Admin. You've got at least two options to do it:

  1. Run your IDE as admin

  2. Update your application manifest as described in this answer

UPD2 apparently you can interface with WFF controller like so

Upvotes: 1

Related Questions