I have a WCF environment hosted on a windows service. I have two hosts (one on TCP and another on NamedPipes). Don’t be alarmed about the multiple hosts – this is a messaging engine and hosts are mounted dynamically based on the configuration. No matter how many types of hosts I have, there is only one service implementation. Now the problem is, when my service implementation is invoked by an incoming call, how do I identify whether it was from host A or host B? While hosting each type, can I specify some metadata which identifies the host information so that I can access it from the implementation? Please help.
Thanks,
James
I found a nice solution for the issue I was facing. Normally when we host a WCF endpoint this is the code we follow.
Here you pass in the type of the interface you expose to the ServiceHost instance. Instead of this approach, you can make use of the second overload of the ServiceHost constructor which takes in an instantiated object! Now the code looks like this
Only thing to note here is that you need to mark your implementation instance mode as a ‘InstanceContextMode.Single‘, effectively making it a Singlreton.
Now the way it solves my problem is that I use my implementation class to pass any metadata from the host to the implementation. My code now looks like this.
// Just pass in the instance to the host.
// My Implementation looks like this
See that you have all your meta data in the member ‘m_MetaData’.