5

I am making a basic self hosting WCF service, and I was just wondering what the best way is to have it wait while it accepts requests? All of the basic tutorials I have found simply use Console.ReadLine to wait for the user to hit enter to exit. This does not seem like a very practical for a real application. I tried a while(true); loop, but this consumed all available CPU cycles, so it is not an option. I also tried Thread.Sleep(0), but the service will not accept requests while sleeping, so this also didn't work. I'm sure there is some common way to have your program "stall" to wait for WCF requests; anyone know how?

I am using C#, .NET 3.5 sp1.

1
  • It's worth noting that Thread.Sleep(0) only sleeps long enough to let other threads run, then returns. It will not have the effect of waiting for any period of time. You may have indended for Thread.Sleep(Timeout.Infinite) (the value is -1) which blocks indefinitely, but a ManualResetEvent is better than that.
    – Josh
    Commented Jan 12, 2011 at 2:18

3 Answers 3

7

If you have this running in a separate thread (since its self hosted), an easy option is to use a ManualResetEvent.

Just call manualResetEvent.WaitOne(); in the WCF thread. This will block (like Console.ReadLine) until manualResetEvent.Set() gets called from a separate thread.

The nice thing here is that you can have a clean mechanism for shutting down the service, as well.

1
  • beautiful. That is exactly what I was looking for. I will set this as the accepted answer as soon as possible. The shutdown mechanism is a great help as well. Commented Jan 12, 2011 at 2:15
7

A real application, if it doesn't have a UI, would probably be better off as a Windows Service. You could set up the WCF service host in the OnStart method of the service and then tear it down in the OnStop.

The reason the examples usually use a console application is because it's easy to demonstrate without confusing the reader with unrelated code to get the service installed and running. But if your server isn't going to have an interactive UI, I would suggest investigating the Windows Service project template.

2
  • 1
    He mentioned that it's a self-hosted service. There are many practical uses of this, including putting a WCF service hosted within a Windows Forms app, etc. This typically involves setting up a thread to host the WCF service, and using some technique like a wait handle to prevent it from shutting down... Commented Jan 12, 2011 at 2:17
  • 3
    A self-hosted service simply means he's bootstrapping WCF instead of IIS or some other host process. A Windows Service is the recommended type of application for a non-interactive self-hosted service.
    – Josh
    Commented Jan 12, 2011 at 2:21
1

It is easy to get a WCF service to run in a console app. I could not get a self-hosted WCF to work in a windows service. Probably too many security problems to deal with. To improve on the console-app service-hosting samples I make an AttachService method that runs on it's own thread like this.

public static AutoResetEvent manualReset;

// Host the service within this EXE console application.
public static void Main()
{
  manualReset = new AutoResetEvent(false);

  ThreadPool.QueueUserWorkItem(AttachService);

  //put Set() signal in your logic to stop the service when needed
  //Example:
  ConsoleKeyInfo key;
  do
  {
    key = Console.ReadKey(true);
  } while (key.Key != ConsoleKey.Enter);
  manualReset.Set();
}
static void AttachService(Object stateInfo)
{
  // Create a ServiceHost for the CalculatorService type.
  using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), new Uri("net.tcp://localhost:9000/servicemodelsamples/service")))
  {
    // Open the ServiceHost to create listeners and start listening for messages.
    serviceHost.Open();

    // The service can now be accessed.

    //Prevent thread from exiting
    manualReset.WaitOne(); //wait for a signal to exit
    //manualReset.Set();
  }
}

My aim is to execute this console app from a Windows service using Process class in the OnStart method. Thanks @Reed Copsey for the suggestion on WaitOne().

1
  • Tested and works from Process.Start(). In Windows 10, it only works when the Windows Service runs under ServiceAccount.LocalSystem. Otherwise I can't get past this exception using NET.TPC binding: System.ServiceModel.AddressAccessDeniedException at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen(). You also need to run Process.Kill() from the OnStop() override or the WCF service will remain running on the system after the Windows Service is stopped. Commented Jun 30, 2017 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.