title: WCF custom serialization using an endpoint behavior
body: <p>I have a service that runs on a LAN with no clients other than the ones I create. I have been attempting to remove the default DataContractSerializerOperationBehavior and replacing it with a custom one I have written and thus far have been wildly unsuccessful. I am using C# 4.5 and run on NetTcpBindings.</p>
  
  <p>I wrote the following 3 classes using protobuf-net's implementation as an example. I will only include what I consider to be the main parts of the classes. If more detail is required then I will provide it.</p>
  
  <p>CustomXmlObjectSerializer : XmlObjectSerializer</p>
  
  <p>CustomSerializerOperationBehavior : DataContractSerializerOperationBehavior </p>
  
  <pre><code>public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList&lt;Type&gt; knownTypes) {
      return new CustomXmlObjectSerializer();
  }
  
  public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList&lt;Type&gt; knownTypes) {
      return new CustomXmlObjectSerializer();
  }
  </code></pre>
  
  <p>CustomEndpointBehavior : IEndpointBehavior </p>
  
  <pre><code>private void ReplaceDataContractSerializerOperationBehavior(ServiceEndpoint endpoint) {
      foreach (var operation in endpoint.Contract.Operations)
          ReplaceDataContractSerializerOperationBehavior(operation);
  }
  
  private void ReplaceDataContractSerializerOperationBehavior(OperationDescription operation) {
      for (var i = 0; i &lt; operation.Behaviors.Count; i++) {
          if (operation.Behaviors[i] is DataContractSerializerOperationBehavior) {
              operation.Behaviors[i] = new CustomSerializerOperationBehavior(operation);
              break;
          }
      }
  }
  </code></pre>
  
  <p>The instantiation of the service host looks like this:</p>
  
  <pre><code>serviceHost = new ServiceHost(typeof(ImplementationType));
  var contract = typeof(ContractType);
  var binding = GetBinding(configuration.Address, configuration.MaxTransferSize * 1024 * 1024);
  var endpoint = serviceHost.AddServiceEndpoint(contract, binding, configuration.Address);
  endpoint.Behaviors.Add(new CustomEndpointBehavior());
  serviceHost.Open();
  </code></pre>
  
  <p>The instantiation of the service client looks like this:</p>
  
  <pre><code>var binding = GetBinding(configuration.Address, configuration.MaxTransferSize * 1024 * 1024);
  var endpoint = new EndpointAddress(configuration.Address);
  factory = new ChannelFactory&lt;ContractType&gt;(binding, endpoint);
  factory.Endpoint.Behaviors.Add(new CustomEndpointBehavior());
  </code></pre>
  
  <p>When I do this I can see the CustomEndpointBehavior replacing the DataContractSerializerOperationBehavior with my CustomSerializerOperationBehavior however I never actually see the calls to create the CustomXmlObjectSerializer (and subsequent usage of it).</p>
  
  <p>I have tried switching my binding from NetTcpBinding to BasicHttpBinding and have seen no change in behavior.</p>
  
  <p>Does anyone have any insight as to why, even after observing each operation's default DataContractSerializerOperationBehavior being replaced with the CustomSerializerOperationBehavior I don't actually see a change in the serialization behavior?</p>