How to use Custom Serialization or Deserialization in WCF to force a new instance on every property of a datacontact ?
Asked Answered
C

1

9

I have a datacontact with many members that has a custom class

I would like to force a new instance if the property is null on deserialization.

is there a way to do that?

Cenacle answered 10/11, 2011 at 19:0 Comment(3)
This is why DTOs are nice to have around (also makes up-revs/changes in schema easier).Bicycle
Take a look at: https://mcmap.net/q/832710/-wcf-service-default-valuesClamper
do you mean datacontact or datacontract ?Egotist
G
14

If your are using DataContract serialization then you can override its default behaviour using the OnDeserialized attribute.

From MSDN: When applied to a method, specifies that the method is called during deserialization of an object in an object graph. The order of deserialization relative to other objects in the graph is non-deterministic.

Here is my sample code:

namespace MySpace
{

  public class MyCustomClass
  {
    public string MyStrData { get; set; }
  }

  [DataContract]
  public class Data
  {
    [DataMember]
    public int mInt;

    [DataMember]
    public MyCustomClass MyCustonObj;



    [OnDeserialized]
    void OnDeserialized(StreamingContext c)
    {
      if (MyCustonObj == null)
      {
        MyCustonObj = new MyCustomClass();
        MyCustonObj.MyStrData = "Overridden in serialization";
      }
    }

    [OnDeserializing]
    void OnDeserializing(StreamingContext c)
    {
      if (MyCustonObj == null)
      {
        MyCustonObj = new MyCustomClass();
        MyCustonObj.MyStrData = "Overridden in  deserializing";
      }
    }

    [OnSerialized]
    void OnSerialized(StreamingContext c)
    {
       // if you wan to  do somehing when serialized here or just remove them

    }

    [OnSerializing]
    void OnSerializing(StreamingContext c)
    {
       // if you wan to  do somehing during serializing here or just remove them    
    }
  }


}
[ServiceContract]
public interface IService
{
  [OperationContract]
  Data Method(Data dd);
}

public class Service : IService
{
  public Data Method(Data dd)
  {
    return dd;
  }
}

class Program
{
  static void Main(string[] args)
  {
    string Url = "http://localhost:8000/";
    Binding binding = new BasicHttpBinding();
    ServiceHost host = new ServiceHost(typeof(Service));
    host.AddServiceEndpoint(typeof(IService), binding, Url);
    host.Open();
    ChannelFactory<IService> fac = new ChannelFactory<IService>(binding);
    fac.Open();
    IService proxy = fac.CreateChannel(new EndpointAddress(Url));
    Data d = new Data();
    d.mInt = 5;
    Console.WriteLine("Data before calling service " + d.mInt);
    Console.WriteLine("Data before calling service " + (d.MyCustonObj == null ? "null" : d.MyCustonObj.MyStrData));
    d = proxy.Method(d);
    fac.Close();
    host.Close();
    Console.WriteLine("Data after calling service " + d.mInt);
    Console.WriteLine("Data after calling service " + d.MyCustonObj.MyStrData);
    Console.ReadLine();
  }
}
Genip answered 10/11, 2011 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.