Click here to Skip to main content
15,890,382 members
Articles / Programming Languages / C#

[C#] WCF: Pass Meta Data from Host to Implementation

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
30 Apr 2012CPOL1 min read 8.5K   3  
Pass meta data from host to implementation

I have two WCF 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?

Well, I need to pass some meta data from the host to my implementation class. How do I do it? There are two ways:

Using an Instance of the Implementation

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

C#
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:

C#
ServiceImplementationimplementation1 = 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 Singleton.

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:

C#
// Create a metadata class just to hold your data. 
public classMetaData      
{      
   public MetaData(stringdata1,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 = MetaData("D1", 100);
ServiceImplementation implementation1 = new ServiceImplementation(metadata);
ServiceHost serviceHost = new ServiceHost(implementation1);

// My Implementation looks like this 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

internal classServiceImplementation : IService 
{
   private MetaDatam_MetaData;
   public ServiceImplementation(MetaDatametaData)
   {
        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’.

2. Using a Derivation of ServiceHost

Create a derivation from the 'ServiceHost' class with some additional information:

C#
public classServiceHostWithMetaData : ServiceHost 
{      
   private MetaDatam_MetaData;  
   public MetaData MetaData
   {
       get { return m_MetaData; }
   }

   public ServiceHostWithMetaData(MetaDatametaData, TypeserviceType, params Uri[]) 
   {      
          m_MetaData = metaData;     
   }      
}

Now you can access your metadata in the following way:

C#
ServiceHostWithMetaData hostWithMetaData = 
     (ServiceHostWithMetaData)OperationContext.Current.Host;
MetaData metaData = hostWithMetaData.MetaData;

This should get you going.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --