DataContract not serializing

Only different...

I can make this post really quick: For a class to be serialized you need to use it in a function. But what would the fun of that be?

So, have you every had the problem where you decorated your class which you want to serialize with one of these puppies: “<DataContract()>” ,  and the little bugger just does not want to serialize on the client? What’s up with that huh? Well I ran into this ‘problem’ when I first started out with WCF. Notice the parenthesis? Because it’s really not a problem, it’s by design. I’ll show you.

When you start a fresh new WCF Service Application in Visual Studio and you check the interface from IService1 it looks like this in VB.NET:

‘ NOTE: You can use the “Rename” command on the context menu to change the interface name “IService1” in both code and config file together.
<ServiceContract()> _
Public Interface IService1

<OperationContract()> _
Function GetData(ByVal value As Integer) As String

<OperationContract()> _
Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType

‘ TODO: Add your service operations here

End Interface

‘ Use a data contract as illustrated in the sample below to add composite types to service operations.

<DataContract()> _
Public Class CompositeType

Private boolValueField As Boolean
Private stringValueField As String

<DataMember()> _
Public Property BoolValue() As Boolean
Return Me.boolValueField
End Get
Set(ByVal value As Boolean)
Me.boolValueField = value
End Set
End Property

<DataMember()> _
Public Property StringValue() As String
Return Me.stringValueField
End Get
Set(ByVal value As String)
Me.stringValueField = value
End Set
End Property

End Class

Now leave the decorated class ‘CompositeType’, but set the decorated function ‘GetDataUsingDataContract’ in comment (VS will start whining about the fact that the interface is not implemented properly so you’ll have to comment the function in the class Service1 too) . Start a new client (like a Win application or whatever) and add a ServiceReference to our newly created service (find my other post if you can’t get the service reference up and running) and notice that CompositeType is not, I repeat not, serialized on the clientside. What gives?

Well because CompositeType is not used in any function, which is decorated with the <OperationContract()> tag, VS in (all it’s wisdom) ‘decides’ you don’t need it. Which is kind of true…

So, uncomment the function GetDataUsingDataContract. Uncomment the rest in the class Service1. Update your reference to the service and voila, there she is: CompositeType.

Hope this helps. ‘Till my next post!


About Danny

Bachelor in Commercial ICT MCTS Winforms .NET 2.0 MCTS ASP.NET 3.5 PSM I
This entry was posted in .NET programming, WCF and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s