Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with using IEnumerable types as return types in WCF

I have been trying since some days now to find a concrete answer for why IEnumerable types cause weird errors while using them as return types in operation contracts.

I came across lot of articles and forums posting but what I have till now is how to prevent errors with other alternative solutions like array of objects or generic list List.

I would like community members to suggest more relevant posting or any reference material that would explain this WCF behaviour.

like image 815
Kunal Avatar asked May 02 '11 04:05

Kunal


1 Answers

WCF uses a message passing system - it serializes calls and return values into XML serialized messages.

As such, it can only deal with stuff that can be expressed in XML schema - and interfaces aren't expressible in XML schema.

Try using a concrete type (a List<T> or an array) instead - those should work just fine.

There are some ways to get around this - but you sacrifice any interoperability with non-.NET clients in the process: you could use the NetDataContractSerializer (see this blog post and the article by Aaron Skonnard on NetDataContractSerializer); with this, you basically embed additional .NET runtime info in your serialized messages. This will make your messages bigger, and any non .NET client won't understand this, but if you're controlling both ends of the wire and both ends are .NET only, then this might work as a workaround.

This also supports using interfaces as your service method parameters - not sure about return types, though.

I don't typically recommend this - but depending on your situation and your needs, this might be an alternative for you. Check it out!

like image 181
marc_s Avatar answered Oct 26 '22 23:10

marc_s