Я пытаюсь обеспечить сохранение моих ссылок на объекты при сериализации через WCF, но CreateSerializer
в моем переопределенном DataContractSerializerOperationBehavior
никогда не вызывается.Кто-нибудь может мне помочь с этим.
Это мой код:
Custom DataContractSerializerOperationBehavior:
public class ObjectGraphDataContractSerializerBehavior : DataContractSerializerOperationBehavior
{
/// <summary>
/// Initializes a new instance of the <see cref="ObjectGraphDataContractSerializerBehavior"/> class.
/// </summary>
/// <param name="operationDescription">The operation description.</param>
public ObjectGraphDataContractSerializerBehavior(OperationDescription operationDescription)
: base(operationDescription)
{
}
public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
{
return new DataContractSerializer(type, name, ns, knownTypes,
int.MaxValue /*maxItemsInObjectGraph*/,
false/*ignoreExtensionDataObject*/,
true/*preserveObjectReferences*/,
null/*dataContractSurrogate*/);
}
public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
{
return new DataContractSerializer(type, name, ns, knownTypes,
int.MaxValue /*maxItemsInObjectGraph*/,
false/*ignoreExtensionDataObject*/,
true/*preserveObjectReferences*/,
null/*dataContractSurrogate*/);
}
public static void SetDataContractSerializerBehavior(ContractDescription contractDescription)
{
foreach (OperationDescription operation in contractDescription.Operations)
{
operation.Behaviors.Add(new ObjectGraphDataContractSerializerBehavior(operation));
}
}
}
Запуск хоста службы:
var serviceHost = new ServiceHost(typeof (Service));
serviceHost.AddServiceEndpoint(typeof (IService), new NetTcpBinding(), DummyUrl);
foreach (var endpoint in serviceHost.Description.Endpoints)
ObjectGraphDataContractSerializerBehavior.SetDataContractSerializerBehavior(endpoint.Contract);
serviceHost.Open();
Создание канала:
m_ChannelFactory = new ChannelFactory<IService>(m_Binding, new EndpointAddress(m_Url));
ObjectGraphDataContractSerializerBehavior.SetDataContractSerializerBehavior(m_ChannelFactory.Endpoint.Contract);
foreach (var behavior in endpointBehaviors)
m_ChannelFactory.Endpoint.Behaviors.Add(behavior);