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

南笙酒味 提交于 2019-12-18 04:51:06

问题


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 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> ?


回答1:


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




回答2:


You have to use the ServiceKnownTypes attribute.

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace Test.Service
{
    [ServiceContract(Name = "Service", Namespace = "")]
    public interface IService
    {
        [OperationContract]
        [WebInvoke(
            Method = "GET",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        [ServiceKnownType(typeof(List<EventData>))]
        IEnumerable<EventData> Method(Guid userId);
    }
}

Basically you need to know the concrete type of what you're returning. Pretty simple.




回答3:


You need to pass interoperable collections to and from your WCF methods.

WCF plays best with simple types and arrays.

Pass in an array from your client and then turn it into an IEnumerable in the service.

Stuff like IEnumerable is not interoperable, which is what WCF is trying to be.

There may be a way to get around it with known types, but I always strive to make my components as flexible as possible.



来源:https://stackoverflow.com/questions/2068897/cannot-serialize-parameter-of-type-system-linq-enumerable-when-using-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!