Skip to content

Unexpected [nasty!!!!] problem utilising [Serializable] & [DataContract] together in WCF

I have just been fighting a problem for a great amount of time in WCF whereby a custom object of mine was not being sent across the wire in WCF. Actually let me re-phrase, the object was being sent, instantiated, but all properties were empty.

I did an isolated test, instantiated the object, populated only one property in my custom object with a smiple value (“99″), sent it across the wire to the WCF client, and the value was “0″.

VERY STRANGE.

After trying everything and clutching at straws, I removed my class attributes from:

[DataContract]
[Serializable]
public partial class Client { ... }

to:

[Serializable]
public partial class Client {...}

and voilà, it worked. My object was being passed with values populated correctly.

SO WHAT WE LEARNT TODAY

It appears you cannot mark a Data Contract class in use for WCF with both these attributes in conjunction with each other. Because I am relatively new to WCF, <1 year this may be perfectly valid, who knows.

However the interesting point is, this appears to be a new “feature” since the .NET 3.5 Service Pack 1 update, as a friend who is using pre-SP1 utilises both these attributes in production with no problems. Sigh, I always get the weird ones.

So there you have it, and as to why I used both attributes; the reason being is due to some xml serialization I had to perform later on in the client UI, but just marking with Serializable alone as stated works a treat.

Thanks WCF!

See my rant here for further details: http://stackoverflow.com/questions/493827/custom-object-returning-as-empty-from-wcf/
VN:F [1.9.1_1087]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.1_1087]
Rating: 0 (from 0 votes)
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

2 Comments

  1. TH

    Did you set the [DataMember] attribute for the properties that you wanted sent to the client?

    VA:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 29-Apr-09 at 2:54 am | Permalink
  2. Chris

    Thx TH.
    Run into the same Problem, except for: I dont use [Serializable] because its obsolete with WCF and a [DataContract].

    I Added the [DataMember] Attribute and everything works fine.

    VA:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 05-Jun-09 at 2:01 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*
My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...