How to identify the WCF host from service implementation

Go To StackoverFlow.com

0

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

2012-04-05 22:03
by James Poulose
WCF already supports a messaging engine called MSMQ. Any reason you can't use this - tom redfern 2012-04-07 13:50


0

I found a nice solution for the issue I was facing. Normally when we host a WCF endpoint this is the code we follow.

ServiceHost serviceHost = new ServiceHost(typeof(IService))

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

ServiceImplementation implementation1 = new ServiceImplementation();
ServiceHost serviceHost = new ServiceHost(implementation1);

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.

// Create a metadata class just to hold your data.
public class MetaData   
{   
    public MetaData(string data1,int data2) 
    {   
        Data1 = data1;  
        Data2 = data2;  
    }

    public string Data1 { get; set; }   
    public int Data2 { get; set; }  
}

// Just pass in the instance to the host.

MetaData metaData = new MetaData("D1", 100);
ServiceImplementation implementation1 = new ServiceImplementation(metaData);
ServiceHost serviceHost = new ServiceHost(implementation1);

// My Implementation looks like this

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
internal class ServiceImplementation : IService 
{   
    private MetaData m_MetaData;    
    public ServiceImplementation(MetaData metaData) 
    {   
        m_MetaData = metaData;  
    }   

    public string Ping(string name) 
    {   
        return m_MetaData.Data1;    
    }   
}

See that you have all your meta data in the member ‘m_MetaData’.

2012-04-09 15:02
by James Poulose


1

Calling OperationContext.Current.Host within your service implementation will give you access to the specific host object which is hosting the call.

If you want metadata in addition to the type and Hashcode of the host object, you can derive your own service host type from System.ServiceModel.ServiceHost and give it some properties to hold that metadata.

For example:

public class MyServiceHost : ServiceHost
{
    public string Name { get; private set; }

    public MyServiceHost(string name, Type serviceType, params Uri[] baseAddresses) 
       : base(serviceType, baseAddresses) 
    {
        this.Name = name;
    }
}
2012-04-06 13:17
by Chris Dickson
I like your solution by creating a derivation of ServiceHost. Had i not found the method which i have posted, i would have used yours. I think when we cannot make the service implementation a Singleton, your solution will be good. Thanks - James Poulose 2012-04-09 15:04
Ads