Want to replace wcf serializer with a custom one. After googling I've found examples. But it do not work. Here is my code: Substitutor:
internal class MySerializerSubstitutor : DataContractSerializerOperationBehavior
{
private static readonly MySerializer _serializer = new MySerializer();
public MySerializerSubstitutor (OperationDescription operationDescription)
: base(operationDescription)
{
}
public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
{
return _serializer; //NEVER CALLED
}
public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
{
return _serializer; // NEVER CALLED
}
}
Behavior which repolace serializer
public class MySerializerBehavior : IOperationBehavior
{
.......
public void ApplyDispatchBehavior(OperationDescription description, DispatchOperation dispatch)
{
var dcs = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
if (dcs != null)
description.Behaviors.Remove(dcs);
description.Behaviors.Add(new MySerializerSubstitutor(description)); //works fine
}
.............
}
And host:
protected override void ApplyConfiguration()
{
var behavior = new MySerializerBehavior()
foreach (var op in Description.Endpoints.SelectMany(ep => ep.Contract.Operations))
{
op.Behaviors.Add(behavior);
}
}
Whats wrong with this code?