I have created a WCF Service Library in VS2010 and can run the service by selecting Debug->Start New Instance from project's right click menu.
Can I further define some initia开发者_开发问答lization logic that would be executed before the service is started?
EDIT:
What I try to achieve is to initialize NHibernate and several other elements so that they are ready when the service starts.
If you self-host (i.e. write your WCF host yourself) - sure, no problem, do whatever you need to do before you call .Open()
on the ServiceHost
.
ServiceHost host = new ServiceHost(typeof(YourServiceClass));
// do your initialization here
........
host.Open();
If you're using IIS or WAS or AppFabric to host your WCF service: I doubt it, since those are "message-based" activation server, e.g. they start up a service host to handle a request on demand, when a request comes in, and I'm not aware of any extension points to get into the initialization process if you're using the regular ServiceHost
class for hosting.
That said: you can of course define your own descendants of ServiceHost
- derive your custom service host from ServiceHost
or ServiceHostBase
- those should give you points to get into the initialization process (overriding the InitializeRuntime
method, or responding to the Opening
event).
See the MSDN docs on:
- ServiceHostBase abstract base class for custom service hosts
- ServiceHost as a concrete implementation of
ServiceHostBase
to possibly inherit from - Custom Service Host explaining the ins and outs of how to achieve this all
精彩评论