简体   繁体   中英

Best way to wait for WCF service?

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.

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.

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.

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().

使用BcakgroundWorker它是一个非常好的解决方案。

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