Click here to Skip to main content
11,411,494 members (65,669 online)
Click here to Skip to main content

WCF Serialization

, 9 Feb 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
WCF Serialization does not call the constructor


This post is all about WCF serialization surprising behavior

As you may know while passing complex type as parameter for WCF will be serialized using DataContractSerializer.

One thing you should notice about the serialization process is that unlike XmlSerializer it will not call the constructor or field initialization.

The code sample for this post is available here

What it all about

If you declare the following contract

public class MyCompositeType
  private Guid m_id1 = Guid.NewGuid ();<span style="COLOR: #009900"> // will not happens during serialization</span>
  public MyCompositeType () <span style="COLOR: #009900">// will not happens during serialization</span>
    Id2 = Guid.NewGuid ();
  public Guid Id1
    get { return m_id1; }
  public Guid Id2 { get; private set; }

  public string StringValue { get; set; }

Do not expect that the ids (which is not data member) be initialized on the other side of the serialization process

If we having the following service implementation:

public MyCompositeType GetDataUsingDataContract ( MyCompositeType composite )
  composite.StringValue += " : " + composite.Id1.ToString () + " : " + composite.Id2.ToString ();
  return composite;

The call it from the client look like this:

static void Main ( string[] args )
  MyProxy.SampleServiceClient svc = new MyProxy.SampleServiceClient ();
  var composite = new MyCompositeType { StringValue = "Test" };

  Console.WriteLine ("Id1 = {0} \nId2 = {1}",
  composite.Id1.ToString (), composite.Id2.ToString ());

  var response = svc.GetDataUsingDataContract (composite);

  Console.WriteLine (response.StringValue);
  Console.WriteLine ("Id1 = {0} \nId2 = {1}",
  response.Id1.ToString (), response.Id2.ToString ());

  Console.ReadLine ();

The ids will be initialized to none empty Guid only on the client side
Both at the server and the response ids will be empry

You can place brake point on the complex type constructor at the server side
but it will never stop there.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Bnaya Eshet
Chief Technology Officer Wise Mobility
Israel Israel
C.T.O and co-founder at wise-mobility l.t.d

Comments and Discussions

General:) i appreciate the solid underline knowlage of some of you PinmemberBnaya Eshet11-Feb-09 7:38 
GeneralWrong PinmemberSarafian9-Feb-09 22:10 
I believe all of you are wrong.

The key is whether you have the bussiness objects in a common assembly or not. If you do then wehn creating the WCF Service Reference you can choose to reuse that types. If you don't the bussiness objects are created through whatever the metadata can export from them. Obviously it won't extract field data or code withing the geter or seter of properties.
But if you do use the same code, then nothing new is created, so the same code will be used.So everything will be as expected.

You must understand that there is no possibility that a class instance will be created in a .NET world without calling a constructor.
The error in the writer is that he never took a look at the type of the object that the service returned.
They are simply not the same.

Personally I find incredibly lazy not to use the same code for the transfering objects.
GeneralRe: Wrong Pinmemberspringy7610-Feb-09 2:15 
GeneralRe: Wrong PinmemberSarafian10-Feb-09 3:56 
GeneralRe: Wrong PinmemberMember 379640310-Feb-09 21:32 
GeneralRe: Wrong PinmemberSarafian10-Feb-09 22:03 
GeneralMy vote of 1 PinmemberHellcat829-Feb-09 20:52 
QuestionHow did this article even get approved? PinmemberMark Nischalke9-Feb-09 18:33 
GeneralMy vote of 1 PinmemberMark Nischalke9-Feb-09 18:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 9 Feb 2009
Article Copyright 2009 by Bnaya Eshet
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid