Take the 2-minute tour ×

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 functionality to another WCF service and host them both inside of a windows service?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

The service class (the one implementing your service interface) has a property


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!

share|improve this answer
Didn't know I could do that. Pretty nifty. –  InTheFlatField Feb 11 '10 at 17:27
It is required to cast the reference returned by OperationContext.Current.Host to MyCustomServiceHost class. And since my wcf service project is referenced by windows service project this custom ServiceHost class has to be inside wcf project. So it gets tricky, unless I am missing something. –  maulik13 Feb 24 at 14:43
@Maulik13: if you want to use custom features of your own MyCustomServiceHost - then yes, you must cast the reference to a variable of that type. And NO, the custom service host class doesn't have to be inside the WCF project - it can basically live anywhere (of course it needs a reference to the WCF project - to know what it's hosting) –  marc_s Feb 24 at 14:45
@marc_s The only extra thing I had to do was to take MyCustomerServiceHost and keep it outside into a third project to avoid circular references. I already reference WCF project into my windows service project. –  maulik13 Feb 24 at 19:25

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.

share|improve this answer

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).

share|improve this answer

Your Answer


