Friday, April 13, 2012

[C#] Validate a method’s signature in run time

Usage of interfaces is always the recommended way to enforce a method’s signature – there are no doubts in that. But there are some occasions where you cannot use an interface to achieve this.
I had a scenario this week, in which I had to use some methods dynamically by reflecting them in run time. Basically these assemblies are created by others and I just consume the methods in run time.  To add more problem, there can be any number of these methods. Even though there is an agreed signature for these methods, often times people can forget those ‘trivial’ details, resulting in run time exceptions, which may not be a nice thing to happen in a production system J.
So I wrote a little validation method which will validate a specified method against my signature. First define a delegate which matches your requirement.
delegate bool MyDesiredSignature(string param1, string param2);

Now extract the method info from the user’s assembly and attempt to create a delegate which matches my delegate (in this case it will be MyDesiredSignature).
bool ValidateSignature(string methodName, object typeInstance, Type delegateType)
      MethodInfo methodInfo = this.GetType().GetMethod(
            methodName, // The method's name.
            BindingFlags.Instance | BindingFlags.NonPublic); // Change this accordingly for static/public methods.

      // Attempt to create a delegate of my delegate type from the method info.
      Delegate methodDelegate = Delegate.CreateDelegate(delegateType, typeInstance, methodInfo, false);

      // If we have 'methodDelegate' as null, then that means the delegate does not match our signature
      // or else we have a match.
      return (methodDelegate != null);

Now we can just call this method for method verification like this.

bool isValidSignature = ValidateSignature("UsersMethodName", this, typeof(MyDesiredSignature));

Happy coding!
James Poulose

Monday, April 9, 2012

[C#] WCF: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
  1.  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.

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 = 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’.

2. Using a derivation of ServiceHost

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

public class ServiceHostWithMetaData : ServiceHost
       private MetaData m_MetaData;  
       public MetaData MetaData
           get { return m_MetaData; }

       public ServiceHostWithMetaData(MetaData metaData, Type serviceType, params Uri[])
              m_MetaData = metaData;    

  Now you can access your metadata in the following way.

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

This should get you going.