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+WhereSelectEnumerableIterator
2[Cheeso.Samples.Webservices._2010.Jan.User,System.Guid]' (for operation 'GetAllUsers', contract 'IJsonService') because it is not the exact type 'System.Collections.Generic.IEnumerable
1[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>
?
No, one must return a concrete class from a web service. Make the return type List and be done with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With