tags:

views:

38

answers:

1

I have a WCF service in which I would like to do some initialization-type operations based on the configured EndpointAddresses for a few different contracts implemented by the service.

The service can be (and is) hosted from within a few different Service Hosts. There is a console application which creates a service host, a windows service which creates a service host, it lives in an IIS host and I would also really like to be able to use the Visual Studio service host for debugging.

Is there any way to get a reference to the ServiceHostBase which created the instance of the service without being inside a service operation? Or maybe a better (read: trickier) way of figuring out what endpoints the service is servicing?

+1  A: 

Let me see if I have this straight: You have a single Service implementation that is exposed from multiple ServiceHosts, and you want to do some different initialization for each servicehost? Or is it for each endpoint exposed?

It sounds to me like there are a few options here, but it depends on exactly what you want to do. If the intialization is per-host, then why not just use your own ServiceHost implementation and do the initialization there instead of the service?.

I ask this particularly because it is not clear from your description what the instance mode of your service is or when you want to run the initialization code itself.

If for whatever reason you can't do that, another option worth exploring could be to do the initialization in a custom IServiceBehavior during ApplyDispatchBehavior(), where you've got access to the service host and the service description.

tomasr
It runs in single-instance mode. I have been trying to avoid using a custom ServiceHost just because we love using the WCF Service Host app for testing and don't know of a way to use a custom ServiceHost class with that.I was looking into how to accomplish it within ApplyDispatchBehavior(), but I have yet to wrap my head around it.
MojoFilter
Holy cow, it really was that easy. I guess what was tripping me up is that for some reason my broken brain had come to the conclusion that I couldn't add an additional ServiceBehavior to the one which was already defined.
MojoFilter