How to check the availability of a net.tcp WCF service

前端 未结 5 1624
失恋的感觉
失恋的感觉 2020-12-14 03:10

My WCF server needs to go up and down on a regular basis, the client sometimes uses the server, but if it is down the client just ignore it. So each time I need to use the s

5条回答
  •  时光说笑
    2020-12-14 04:03

    Here's what I'm using and it works like a charm. And btw, the ServiceController class lives in namespace 'System.ServiceProcess'.

    try
    {
        ServiceController sc = new ServiceController("Service Name", "Computer's IP Address");
        Console.WriteLine("The service status is currently set to {0}",
            sc.Status.ToString());
    
        if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
            (sc.Status.Equals(ServiceControllerStatus.StopPending)))
        {
            Console.WriteLine("Service is Stopped, Ending the application...");
            Console.Read();
            EndApplication();
        }
        else
        {
            Console.WriteLine("Service is Started...");
        }
    }
    catch (Exception)
    {
        Console.WriteLine("Error Occurred trying to access the Server service...");
        Console.Read();
        EndApplication();
    }
    

提交回复
热议问题