简体   繁体   English

等待WCF服务的最佳方式?

[英]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? 我正在制作一个基本的自托管WCF服务,我只是想知道最好的方法是让它等待接受请求吗? All of the basic tutorials I have found simply use Console.ReadLine to wait for the user to hit enter to exit. 我找到的所有基本教程都只使用Console.ReadLine来等待用户按Enter键退出。 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. 循环,但这消耗了所有可用的CPU周期,因此它不是一个选项。 I also tried Thread.Sleep(0), but the service will not accept requests while sleeping, so this also didn't work. 我也试过Thread.Sleep(0),但服务在睡觉时不接受请求,所以这也行不通。 I'm sure there is some common way to have your program "stall" to wait for WCF requests; 我确信有一些常见的方法让你的程序“停顿”等待WCF请求; anyone know how? 谁知道怎么样?

I am using C#, .NET 3.5 sp1. 我正在使用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. 如果你在一个单独的线程中运行它(因为它自己托管),一个简单的选择是使用ManualResetEvent。

Just call manualResetEvent.WaitOne(); 只需调用manualResetEvent.WaitOne(); in the WCF thread. 在WCF线程中。 This will block (like Console.ReadLine) until manualResetEvent.Set() gets called from a separate thread. 这将阻止(如Console.ReadLine),直到从单独的线程调用manualResetEvent.Set()

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. 如果没有UI,真正的应用程序可能会更好地作为Windows服务。 You could set up the WCF service host in the OnStart method of the service and then tear it down in the OnStop. 您可以在服务的OnStart方法中设置WCF服务主机,然后在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. 但是,如果您的服务器不具备交互式UI,我建议您调查Windows服务项目模板。

It is easy to get a WCF service to run in a console app. 在控制台应用程序中运行WCF服务很容易。 I could not get a self-hosted WCF to work in a windows service. 我无法让自托管的WCF在Windows服务中工作。 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. 为了改进console-app服务托管示例,我创建了一个AttachService方法,该方法在它自己的线程上运行。

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. 我的目标是使用OnStart方法中的Process类从Windows服务执行此控制台应用程序。 Thanks @Reed Copsey for the suggestion on WaitOne(). 感谢@Reed Copsey对WaitOne()的建议。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM