0

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();
        }
    }
Cui Pengfei 崔鹏飞
  • 8,017
  • 6
  • 46
  • 87
  • can you just check that the process that hosts it is running? http://stackoverflow.com/questions/1276629/c-sharp-get-running-process-given-process-handle – kenny Feb 10 '12 at 00:20
  • @kenny the process is running does not mean the service is started. it usually take a little while for the service to start. – Cui Pengfei 崔鹏飞 Feb 10 '12 at 15:39

1 Answers1

2

You could get the console application to signal an event when its ServiceHost has been opened.


UPDATE

Your starter code should call WaitOne on an instance of your WaitHandle:

EventWaitHandle evtServiceStarted = new EventWaitHandle(...);

Process hostProcess = Process.Start(info); 

//make sure the service is up and running
evtServiceStarted.WaitOne();

// Go ahead and call your service...

Your service host should call Set on a WaitHandle instance pointing to the same named event object:

EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(...);
// Set up the service host and call Open() on it

//... when its all done
eventWaitHandle.Set();

Your service host shouldn't try to open the event more than once - your starter code needs to make sure the event is created (and with the right security permissions) before it kicks off the service application.

Chris Dickson
  • 11,964
  • 1
  • 39
  • 60
  • thank you. I just found out that System.Threading.EventWaitHandle does the same thing. – Cui Pengfei 崔鹏飞 Feb 10 '12 at 16:57
  • BTW EventWaitHandle **is** the same thing ... that is, it is a managed wrapper around a kernel named event object of the sort that the Win32 CreateEvent API creates. – Chris Dickson Feb 10 '12 at 17:31
  • PS - these objects are unmanaged kernel resources which need to be released when you're done with them. Don't forget to call Dispose on them at a suitable time. – Chris Dickson Feb 10 '12 at 17:37