Alex Rivera | Logout

Passing an instance of anonymous type over WCF

Asked 2010-02-21T19:35:19.453
22

I have a WCF service method that expects an object and then retrieves its properties using reflection.

On the client side I create an anonymous type object

var obj = new {FirstName="John", LastName="Doe"}

and pass it to the method. I'm getting an exception:

Type '<>f__AnonymousType0`2[System.String,System.String]' cannot be serialized. 
Consider marking it with the DataContractAttribute attribute, and marking all 
of its members you want serialized with the DataMemberAttribute attribute.  
See the Microsoft .NET Framework documentation for other supported types.

I can't mark the type or its members with serialization attributes because there is really no type nor properties declared explicitly. Is there a way to overcome that problem?

Edit
Report

1 Answer

2

No, there is not. While there are tricks and techniques to return objects of an anonymous type from a method, you cannot send them over WCF.

WCF needs to know all of its concrete types that will be sent across, since you're not really just calling a .NET method. Instead, the message call gets converted into a serialized message, and therefore, any "thing" being passed over a WCF call must be serializable - no exceptions.

answered 2010-02-21T19:38:15.363

Your Answer