简体   繁体   中英

Windows Service hosting WCF - Can I stop Windows Service from within a WCF service?

I have a Windows Service hosting a WCF service. If something goes really wrong in my WCF service I'd like to stop the Windows Service. I could try and force it to stop by shelling out and using net stop but is there a 'nicer' way to do this?

WCF is run the usual way from windows service:

protected override void OnStart(string[] args)
{
    if (serviceHost != null)
    {
        serviceHost.Close();
    }

    ///WCF service hosted
    serviceHost = new ServiceHost(typeof(XXXService));


    serviceHost.Open();
}

In the past, we have implemented our self-hosted WCF Windows Services with a “Service Host Controller” class that holds references to the ServiceHost objects and is therefore responsible for “starting”/opening and “stopping”/closing the service host object. In addition, the “Service Host Controller” class implements a delegate that allows the hosted objects to call back into the controller and initiate a graceful shutdown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM