小编典典

使用WCF,LINQ,JSON时无法序列化'System.Linq.Enumerable…'类型的参数

json

我有WCF服务。它使用Linq-to-objects从字典中进行选择。对象类型很简单:

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

中存储了一个集合Dictionary<Guid,User>

我想要一个这样的WCF OperationContract方法:

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

它可以很好地编译,但是当我运行它时,我得到:

服务器在处理请求时遇到错误。异常消息是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方法签名中的’无法序列化’System.Linq.Enumerable
+ WhereSelectEnumerableIterator 1 [System.Guid]
类型的参数”,并且不在已知的类型集合中。为了序列化参数,请使用ServiceKnownTypeAttribute将类型添加到该操作的已知类型集合中。有关更多详细信息,请参见服务器日志。

如何强制选择成为IEnumerable<Guid>


编辑
如果我修改代码来执行此操作,则效果很好-良好的互操作性。

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

有没有办法让我避免创建/实例化List<T>


阅读 300

收藏
2020-07-27

共1个答案

小编典典

不,必须从Web服务返回一个具体的类。使返回类型为List并完成它。

2020-07-27