Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot serialize parameter of type 'System.Linq.Enumerable... ' when using WCF, LINQ, JSON

I have a WCF Service. It uses Linq-to-objects to select from a Dictionary. The object type is simple:

public class User 
{
   public Guid Id;
   public String Name;
}

There is a collection of these stored in a Dictionary<Guid,User>.

I want to have a WCF OperationContract method like this:

public IEnumerable<Guid> GetAllUsers()
{
    var selection = from user in list.Values
        select user.Id;
     return selection;
}

It compiles fine, but when I run it I get:

The server encountered an error processing the request. The exception message is 'Cannot serialize parameter of type 'System.Linq.Enumerable+WhereSelectEnumerableIterator2[Cheeso.Samples.Webservices._2010.Jan.User,System.Guid]' (for operation 'GetAllUsers', contract 'IJsonService') because it is not the exact type 'System.Collections.Generic.IEnumerable1[System.Guid]' in the method signature and is not in the known types collection. In order to serialize the parameter, add the type to the known types collection for the operation using ServiceKnownTypeAttribute.'. See server logs for more details.

How can I coerce the selection to be an IEnumerable<Guid> ?


EDIT
If I modify the code to do this, it works well - good interoperability.

public List<Guid> GetAllUsers()
{
    var selection = from user in list.Values
        select user.Id;
     return new List<Guid>(selection);
}

Is there a way for me to avoid the creation/instantiation of the List<T> ?

like image 705
Cheeso Avatar asked Jan 15 '10 01:01

Cheeso


1 Answers

No, one must return a concrete class from a web service. Make the return type List and be done with it.

like image 139
Cheeso Avatar answered Oct 02 '22 19:10

Cheeso