Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C#
Article

WCF Serialization

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
9 Feb 2009CPOL 40.4K   5   9
WCF Serialization does not call the constructor
Image 1

Introduction

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

[DataContract]
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; }

  [DataMember]
  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.

License

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


Written By
Chief Technology Officer Wise Mobility
Israel Israel
C.T.O and co-founder at wise-mobility l.t.d
www.wisemobility.com
http://bnaya.blogspot.com/

Comments and Discussions

 
General:) i appreciate the solid underline knowlage of some of you Pin
Bnaya Eshet11-Feb-09 6:38
Bnaya Eshet11-Feb-09 6:38 
As Sarafian mentioned it does relevant for reusing the library of your business entities
Yes there is times when interoperability is needed when the client is not using .NET
but we are talking about the 20%
To those who think it so obvious I can doubt when was the last time that they manage their
.net WCF proxy or looking what does the .net designer has written for them
If you do not reuse the business entity the designer is writing new class that has nothing to do with your business entity except that it has the same public property that marked as data contract, in that case the designer class has default constructor
And now is the time for little history lesson:
as you may remember before WCF there was technology known as web service which used
xml serialization, if you try the same pattern using web service you will find that before
each deserialization the business entity constructor will be invoke

Bnaya Eshet

C.T.O and Chief Architect at
Wise Mobility
www.wisemobility.com

GeneralWrong Pin
aSarafian9-Feb-09 21:10
aSarafian9-Feb-09 21:10 
GeneralRe: Wrong Pin
springy7610-Feb-09 1:15
springy7610-Feb-09 1:15 
GeneralRe: Wrong Pin
aSarafian10-Feb-09 2:56
aSarafian10-Feb-09 2:56 
GeneralRe: Wrong Pin
Member 379640310-Feb-09 20:32
Member 379640310-Feb-09 20:32 
GeneralRe: Wrong Pin
aSarafian10-Feb-09 21:03
aSarafian10-Feb-09 21:03 
GeneralMy vote of 1 Pin
Hellcat829-Feb-09 19:52
Hellcat829-Feb-09 19:52 
QuestionHow did this article even get approved? Pin
Not Active9-Feb-09 17:33
mentorNot Active9-Feb-09 17:33 
GeneralMy vote of 1 Pin
Not Active9-Feb-09 17:32
mentorNot Active9-Feb-09 17:32 

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

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