0

I have console application running from Admin user. I want to restart IIS server using C#. I made this:

        public static void Stop()
        {
            string serviceName = "W3SVC";
            var service = GetService(serviceName);

            if (service == null) return;

            ServiceController sc = new ServiceController(service.ServiceName);

            sc.Stop();
        }
        public static void Start()
        {
            string serviceName = "W3SVC";
            var service = GetService(serviceName);

            if (service == null) return;

            ServiceController sc = new ServiceController(service.ServiceName);

            sc.Start();
        }
        {
            return ServiceController.GetServices()
                .Where(x => x.ServiceName == name).FirstOrDefault();
        }

The problem that when i stop server, sometimes i get this "visual studio just in time debugger error an unhandled exception", even if my visual studio is not open. I deleted this option from visual studio. But it made me think - am i doing restart right way?

Why starting and stoping my way takes like 5-6 seconds, while if i restart server from iis manager, it restarts for 1 sec.

So what is the correct way to restart server from c# code?

ssrdop
  • 29
  • 6
  • 1
    You can run "iisreset.exe" PowerShell command in your app using the following link. https://stackoverflow.com/questions/527513/execute-powershell-script-from-c-sharp-with-commandline-arguments – Commander Sep 28 '21 at 07:23
  • I think its bad idea: https://learn.microsoft.com/vi-VN/troubleshoot/iis/using-iisreset-restart-iis-result-error – ssrdop Sep 28 '21 at 08:20
  • https://stackoverflow.com/questions/7870745/how-can-i-restart-iis-from-c-sharp-code-running-as-a-user-who-is-an-administrato – user2316116 Sep 28 '21 at 11:04

1 Answers1

0

Try to use this code to restart IIS.

using System.ServiceProcess;

using (ServiceController controller = new ServiceController())
{
   controller.MachineName = “My local or remote computer name”;
   controller.ServiceName = “IIS Service Name”; // i.e “w3svc”

   if (controller.Status != ServiceControllerStatus.Running)
   {
      // Start the service
      controller.Start();

       Log.Debug(“IIS has been started successfully, now checking again for 
       webservice availability”);

   }
   else
   {
      // Stop the service
      controller.Stop();

      // Start the service
      controller.Start();

        Log.Debug(“IIS has been restarted successfully”);

    }

  }
Bruce Zhang
  • 2,880
  • 1
  • 5
  • 11
  • Thanks! I wrote code like urs. But ur code will return exceptions sometimes, because when u run "controller.Start()" or "controller.Stop()" u have to wait untill it finished, because it not works on fly – ssrdop Sep 29 '21 at 08:26
  • IIS stop and start is essentially the stop and start of Windows network services. So restarting takes time to wait for the process to respond. Otherwise, the service will easily collapse. – Bruce Zhang Sep 30 '21 at 09:24