What is the best way to get notified when a WCF service is first started?
Is there something similar to the Application_Start method in the Global.asax for an ASP.NET application?
There are three types of hosting environments for WCF services: IIS, WAS, and self-hosting. The term “self-hosting” refers to any application that provides its own code to initialize the hosting environment. This includes console, Windows Forms, WPF, and managed Windows services.
To open WCF Test Client, open Developer Command Prompt for Visual Studio and execute WcfTestClient.exe. Select Add Service from the File menu. Type http://localhost:8080/hello into the address box and click OK. Make sure the service is running or else this step fails.
Viewing Event Logs. Event logging is enabled automatically by default, and there is no mechanism to disable it. Events logged by WCF can be viewed using the Event Viewer. To launch this tool, click Start, click Control Panel, double-click Administrative Tools, and then double-click Event Viewer.
The ServiceHost class creates a ServiceDescription from the service type and configuration information and then uses that description to create ChannelDispatcher objects for each endpoint in the description.
Since it's just a class, you can use a static constructor which will be called the first time the Type is used.
public Service : IContract { public Service(){ // regular constructor } static Service(){ // Only called first time it's used. } }
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.
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.
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??
Marc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With