WCF 应用程序启动事件

问题描述:

在 WCF 服务首次启动时获得通知的最佳方式是什么?

What is the best way to get notified when a WCF service is first started?

是否有与 ASP.NET 应用程序的 Global.asax 中的 Application_Start 方法类似的东西?

Is there something similar to the Application_Start method in the Global.asax for an ASP.NET application?

嗯,这可能有点棘手,因为调用 WCF 服务的首选方式是基于每次调用",例如你真的没有任何东西是开始"的,然后只是闲逛,真的.

Well, that might be a bit tricky since the preferred way of calling WCF services is on a "per-call" basis, e.g. you don't really have anything that's "started" and then just hangs around, really.

如果您在 IIS 或 WAS 中托管您的服务,它甚至是按需加载"您的服务主机 - 当消息到达时,主机会被实例化并处理请求.

If you're hosting your service in IIS or WAS, it's even "on-demand loading" of your service host - when a message arrives, the host is instantiated and handles the request.

如果您是自托管,您可以使用控制台或 Winforms 应用程序 - 因此您可以连接到那里以了解它们何时启动.如果你有一个 Windows 服务来托管你的服务主机,你很可能会覆盖 ServiceBase 类上的 OnStart 和 OnStop 方法 --> 挂在那里.

If you self-host, you either have a console or Winforms app - so you could hook into there to know when they start. If you have a Windows service to host your service host, you most likely override the OnStart and OnStop methods on the ServiceBase class --> hook into there.

问题更多:你到底想完成什么?只是记录或类似的东西,或者你想在内存中建立一些东西来坚持吗?

The question is more: what exactly are you trying to accomplish? Just logging or something like that, or do you want to have something built up in memory to stick around??

马克