title: NetDataContractSerializer and WCF
body: <p>I have a scenario where I need to use the NetDataContractSerializer instead of the DataContractSerializer. </p>
  
  <p>Up until now I have been using this method - <a href="http://www.pluralsight.com/community/blogs/aaron/archive/2006/04/21/22284.aspx" rel="noreferrer">http://www.pluralsight.com/community/blogs/aaron/archive/2006/04/21/22284.aspx</a> - which appears quite simple but according to this <a href="http://social.msdn.microsoft.com/forums/en-US/wcf/thread/cb0c56c0-3016-4cda-a3c7-8826f8cc5bb0/" rel="noreferrer">http://social.msdn.microsoft.com/forums/en-US/wcf/thread/cb0c56c0-3016-4cda-a3c7-8826f8cc5bb0/</a> the approach is incorrect.</p>
  
  <p>Looking around a bit more I found the following - <a href="http://social.msdn.microsoft.com/forums/en-US/wcf/thread/f30ecd17-cac0-4cdc-8142-90b5f411936b/" rel="noreferrer">http://social.msdn.microsoft.com/forums/en-US/wcf/thread/f30ecd17-cac0-4cdc-8142-90b5f411936b/</a></p>
  
  <p>Basically you need to run the following:</p>
  
  <p><strong>So this is what I have on the client side:</strong></p>
  
  <pre><code>ChannelFactory&lt;IPersonService&gt; factory = new ChannelFactory&lt;IPersonService&gt;("WSHttpBinding_IPersonService");
  
  foreach (OperationDescription desc in factory.Endpoint.Contract.Operations)
  {
      DataContractSerializerOperationBehavior dcsOperationBehavior = desc.Behaviors.Find&lt;DataContractSerializerOperationBehavior&gt;();
      if (dcsOperationBehavior != null)
      {
          int idx = desc.Behaviors.IndexOf(dcsOperationBehavior);
          desc.Behaviors.Remove(dcsOperationBehavior);
          desc.Behaviors.Insert(idx, new NetDataContractSerializerOperationBehavior(desc));
          //return true;
  
      }       
  }
  
  
  IPersonService svc = factory.CreateChannel();
  </code></pre>
  
  <p><strong>And on the server side:</strong></p>
  
  <pre><code>myServiceHost = new ServiceHost(typeof(PersonService), baseAddress);
  
  
  foreach (ServiceEndpoint endPoint in myServiceHost.Description.Endpoints)
  {
      foreach (OperationDescription desc in endPoint.Contract.Operations)
      {
          DataContractSerializerOperationBehavior dcsOperationBehavior = desc.Behaviors.Find&lt;DataContractSerializerOperationBehavior&gt;();
          if (dcsOperationBehavior != null)
          {
              int idx = desc.Behaviors.IndexOf(dcsOperationBehavior);
              desc.Behaviors.Remove(dcsOperationBehavior);
              desc.Behaviors.Insert(idx, new NetDataContractSerializerOperationBehavior(desc));
              //return true;
          }
      }
  }
  
  myServiceHost.Open();
  </code></pre>
  
  <p>The problem is that I am using the VS generated client service proxies and the standard VS .svc services. So I'm not controlling the creation of the client proxy or the service and the above code assumes that. </p>
  
  <p>One of the comments in the post mentions that you would be able to implement "IServiceBehavior and another one is IClientBehavior" but I have no idea how to go about taking the above code and changing it. Does anyone have any ideas???</p>
  
  <p>Cheers
  Anthony</p>