title: Dynamic ExpandoObject in WCF
body: <p>Trying to Ping / Pong my service with an operation of enum type process and dynamic data.</p>
  
  <pre><code>[ServiceContract ( CallbackContract = typeof ( iStackoverflowCallBack ) )]
  public interface iStackoverflow
  {
      [OperationContract]
      void Ping ( Process Operation , dynamic Data );
  }
  
  [ServiceContract ( )]
  public interface iStackoverflowCallBack
  {
      [OperationContract]
      void Pong ( Process Operation , dynamic Data );
  }
  </code></pre>
  
  <p><strong>Why</strong> this service having issues connecting ?  </p>
  
  <ul>
  <li>When implementing both interfaces <code>dynamic</code> converted automatically to <code>object</code>.</li>
  <li>When <code>ping</code>ing my service from consumer, the ping doesn't reach the service at all but the service is working properly.</li>
  </ul>
  
  <hr>
  
  <p><strong>Solution :</strong></p>
  
  <pre><code>[DataContract]
  public class SerializableDynamicObject : IDynamicMetaObjectProvider
  {
      [DataMember]
      private IDictionary&lt;string,object&gt; dynamicProperties = new Dictionary&lt;string,object&gt;();
  
      #region IDynamicMetaObjectProvider implementation
      public DynamicMetaObject GetMetaObject (Expression expression)
      {
          return new SerializableDynamicMetaObject(expression, 
              BindingRestrictions.GetInstanceRestriction(expression, this), this);
      }
      #endregion
  
      #region Helper methods for dynamic meta object support
      internal object setValue(string name, object value) 
      {
          dynamicProperties.Add(name, value);
          return value;
      }
  
      internal object getValue(string name) 
      {
          object value;
          if(!dynamicProperties.TryGetValue(name, out value)) {
              value = null;
          }
          return value;
      }
  
      internal IEnumerable&lt;string&gt; getDynamicMemberNames() 
      {
          return dynamicProperties.Keys;
      }
      #endregion
  }
  
  
  public class SerializableDynamicMetaObject : DynamicMetaObject
  {
      Type objType;
  
      public SerializableDynamicMetaObject(Expression expression, BindingRestrictions restrictions, object value) 
          : base(expression, restrictions, value) 
      {
          objType = value.GetType();
      }
  
      public override DynamicMetaObject BindGetMember (GetMemberBinder binder)
      {
          var self = this.Expression;
          var dynObj = (SerializableDynamicObject)this.Value;
          var keyExpr = Expression.Constant(binder.Name);
          var getMethod = objType.GetMethod("getValue", BindingFlags.NonPublic | BindingFlags.Instance);
          var target = Expression.Call(Expression.Convert(self, objType),
                                       getMethod,
                                       keyExpr);
          return new DynamicMetaObject(target,
              BindingRestrictions.GetTypeRestriction(self, objType));
      }
  
      public override DynamicMetaObject BindSetMember (SetMemberBinder binder, DynamicMetaObject value)
      {
          var self = this.Expression;
          var keyExpr = Expression.Constant(binder.Name); 
          var valueExpr = Expression.Convert(value.Expression, typeof(object));
          var setMethod = objType.GetMethod("setValue", BindingFlags.NonPublic | BindingFlags.Instance);
          var target = Expression.Call(Expression.Convert(self, objType),
          setMethod, 
          keyExpr, 
          valueExpr);
          return new DynamicMetaObject(target,
              BindingRestrictions.GetTypeRestriction(self, objType));
      }
  
      public override IEnumerable&lt;string&gt; GetDynamicMemberNames ()
      {
          var dynObj = (SerializableDynamicObject)this.Value;
          return dynObj.getDynamicMemberNames();
      }
  }
  </code></pre>