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.
Public Interface IService1
Function GetData(ByVal value As Integer) As String
Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType
‘ TODO: Add your service operations here
‘ Use a data contract as illustrated in the sample below to add composite types to service operations.
Public Class CompositeType
Private boolValueField As Boolean
Private stringValueField As String
Public Property BoolValue() As Boolean
Set(ByVal value As Boolean)
Me.boolValueField = value
Public Property StringValue() As String
Set(ByVal value As String)
Me.stringValueField = value
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!