Arvind Chaudhary
Arvind Chaudhary

Reputation: 67

how to restart web service on remote server pragmatically in c#

I am not able to find the class which is used to start the web service on remote server using a code like we have for window service.

        var sc = new System.ServiceProcess.ServiceController("mywebsite", "remoteservername");
        sc.Start();
        sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running);
        sc.Stop();
        sc.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped);

Upvotes: 0

Views: 2222

Answers (2)

Arvind Chaudhary
Arvind Chaudhary

Reputation: 67

static void Main(string[] args) {

        try
        {
        using (ServerManager manager = new ServerManager())
        {
            var iisManager = ServerManager.OpenRemote("YourServerName");

            Microsoft.Web.Administration.Site site = iisManager.Sites.Where(q => q.Name.Equals("YourSiteName")).FirstOrDefault();

            if (site.State== site.Start())
            {
                site.Stop();
            }
            else
            {
                site.Start();
            }

            manager.CommitChanges();


            }

        }
        catch (Exception ex)
            {

            }
    }

Upvotes: 0

Yuri
Yuri

Reputation: 2900

Web service is not listed under Windows services. It is running under IIS and to stop/start it you'll need to stop / start Application Pool under which this service is running. If you are planning to do it remotely, WMI needs to be enabled on target server. For your convenience providing a code that will do this for you:

public void PoolAction(String servername, String AppPoolName, String action)
    {
        StringBuilder sb = new StringBuilder();
        ConnectionOptions options = new ConnectionOptions();
        options.Authentication = AuthenticationLevel.PacketPrivacy;
        options.EnablePrivileges = true;
        ManagementScope scope = new ManagementScope(@"\\" +
            servername + "\\root\\MicrosoftIISv2", options);

        // IIS WMI object IISApplicationPool to perform actions on IIS Application Pool
        ObjectQuery oQueryIISApplicationPool =
            new ObjectQuery("SELECT * FROM IISApplicationPool");

        ManagementObjectSearcher moSearcherIISApplicationPool =
            new ManagementObjectSearcher(scope, oQueryIISApplicationPool);
        ManagementObjectCollection collectionIISApplicationPool =
            moSearcherIISApplicationPool.Get();
        foreach (ManagementObject resIISApplicationPool in collectionIISApplicationPool)
        {
            if (resIISApplicationPool["Name"].ToString().Split('/')[2] == AppPoolName)
            {
                // InvokeMethod - start, stop, recycle can be passed as parameters as needed.
                resIISApplicationPool.InvokeMethod(action, null);
            }
        }

Note:

  • Action can contain 'Start', 'Stop' or 'Recycle'
  • Account under which this code will run needs to be admin on the target server.

How to enable WMI on the server enabling WMI on the server

Upvotes: 2

Related Questions