I have a WCF Service that is hosted by a Console Application. The client connects to the Service through a named pipe. And the Console only gets executed when the client needs it, and the console gets killed after the client is done.
Here is the code that starts and calls the service:
Process hostProcess = Process.Start(info);
//make sure the service is up and running
//todo: find out a better way to check if the service is up and running.
Thread.Sleep(200);
EndpointAddress endpointAddress = new EndpointAddress("net.pipe://localhost/test");
NetNamedPipeBinding binding = new NetNamedPipeBinding();
IHostedService service=hannelFactory<IHostedService>.CreateChannel(binding, endpointAddress);
service.Run();
hostProcess.Kill();
I am using Thread.Sleep to make sure the service is up and running, but that is definitely not the right approach to do so.
So, how can I determine if a WCF service that is hosted in a Console application is up and running?
follow up question, how can i wait for the event to be fired without using Thread.Sleep?
private static EventWaitHandle GetEventWaitHandle()
{
try
{
EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(string.Format(serviceStartedEventName, taskIndex));
return eventWaitHandle;
}
catch (Exception)
{
//if we do not sleep here, it may cause a stack over flow exceptoin.
Thread.Sleep(10);
return GetEventWaitHandle();
}
}