1

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);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Behnam Esmaili
  • 5,835
  • 6
  • 32
  • 63
  • 1
    If you just want a Service Window and not take down the whole IIS: Place a file called (exactly!) "app_offline.htm" in the application folder. It will be delivered instead of the actual app as long as it is there. Ideally, it will contain some sort of "We're on maintainance, come back later" screen. See also: [Taking Web Applications Offline with Web Deploy](https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/advanced-enterprise-web-deployment/taking-web-applications-offline-with-web-deploy) – Fildor Dec 17 '19 at 07:24

2 Answers2

1

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

timur
  • 14,239
  • 2
  • 11
  • 32
  • thanks @timur for your response. you mean there are windows services corresponding to my four farm servers running in background and i have to find theme and start / stop theme? – Behnam Esmaili Dec 17 '19 at 06:55
  • To answer you question, yes, windows services can be managed remotely. Given you know host names of your farm servers you should be able to issue stop/start commands using the code above. – timur Dec 17 '19 at 07:20
  • should i put server ip in "{ "appServer1", "webServer1", "webServer2" }" ? – Behnam Esmaili Dec 17 '19 at 08:26
  • I'm not sure if ip addresses work. I guess it won't harm if you give it a go? Hopefully you've got a test bench? – timur Dec 17 '19 at 08:27
  • yea i tested the ip as second argument it was able to construct ServiceController instance but once called stop it threw exception. – Behnam Esmaili Dec 17 '19 at 08:32
  • I suspect it's the part where you will need to run your code as elevated user. Bit having specifics will definitely help – timur Dec 17 '19 at 08:42
  • the exception is "Cannot start service W3SVC on computer 'IP_ADDRESS'." i think it is trying to start / stop service in the specified machine rather than stop sending request to machine. – Behnam Esmaili Dec 17 '19 at 08:46
  • I was able to run the code sample above when I restarted my IDE as admin. mind giving that a go? – timur Dec 17 '19 at 08:54
  • no its not working.but i found another solution.the solution is directly modifying application.Host.Config file – Behnam Esmaili Dec 17 '19 at 10:20
1

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;
    }
}
Jokies Ding
  • 3,374
  • 1
  • 5
  • 10