Can you communicate between a WCF service and the Windows service hosting it?
I have a WCF service I use for configuration stuff hosted in a windows service that will be used to maintain a database. Is there any way that I can access the WCF service inside of the hosting service? Or should I move the database functionalit开发者_如何学编程y to another WCF service and host them both inside of a windows service?
The service class (the one implementing your service interface) has a property
OperationContext.Current.Host
which gives you access to the ServiceHost
instance which is hosting your service. You can access that service host pretty easily.
There's no built-in way to reach beyond the service host and manipulate or query the NT service containing the service host. But you could always create your own custom ServiceHost descendant class which would give you the necessary access to the NT service itself, and then use that custom service host for your service implementation.
With a custom service host, you can basically do whatever you feel is necessary and useful - just create a descendant from ServiceHost
and do whatever you need to do!
If by "access" you mean to ask whether you can call the service, then yes, the service can be a client of itself.
What might be better would be for you to separate the service into those parts that are specific to the fact that it's a web service, and all the other parts that do the real work. Have the Windows Service call the latter parts.
The way I do this is to pass a shared object into the constructor of the WCF Service by using a custom InstanceProvider (this allows you to use non-default constructors for the WCF Service).
The shared object then allows the WCF Service to "talk" with the Windows Service (or any other object that can get access the shared object).
精彩评论