Best way to wait for WCF service?

I am doing a basic WCF service for my own hosting, and I am just wondering what is the best way to wait while it accepts requests? All the basic tutorials I found just use Console.ReadLine to wait for the user to press the enter button to exit. This does not seem very practical for a real application. I tried for a while (true); but it consumed all the available CPU cycles, so this is not an option. I also tried Thread.Sleep (0), but the service will not accept requests during sleep, so this also did not work. I am sure that there is a usual way to make your program wait for WCF requests; Does anyone know how?

I am using C #, .NET 3.5 sp1.

+4
source share
4 answers

If this launch is performed in a separate thread (from the moment of its independent placement), a simple option is to use ManualResetEvent.

Just call manualResetEvent.WaitOne(); in the WCF stream. This blocks (for example, Console.ReadLine) until manualResetEvent.Set() is called from a separate thread.

It's nice that you can have a clean mechanism to disable the service.

+7
source

A real application, if it does not have a user interface, is likely to be better as a Windows service. You can configure the WCF service host in the OnStart method of the service and then tear it off in OnStop.

The reason console applications are commonly used is that it is easy to demonstrate without confusing the reader with unrelated code to install and run the service. But if your server does not have an interactive interface, I would suggest exploring the Windows service project template.

+7
source

It’s easy to get a WCF service to run in a console application. I could not get standalone WCF to work in a windows service. There are probably too many security issues. To improve hosting selection for the console application, I create an AttachService method that runs its own thread on it, similar to 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 goal is to run this console application from a Windows service using the Process class in the OnStart method. Thanks @Reed Copsey for the WaitOne () suggestion.

0
source

use BcakgroundWorker , this is a very good solution.

-1
source

Source: https://habr.com/ru/post/1335183/


All Articles