使用WCF,LINQ,JSON时无法序列化类型'System.Linq.Enumerable ...'的参数 [英] Cannot serialize parameter of type 'System.Linq.Enumerable... ' when using WCF, LINQ, JSON

查看:172
本文介绍了使用WCF,LINQ,JSON时无法序列化类型'System.Linq.Enumerable ...'的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

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

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

我想要这样的WCF OperationContract方法:

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:

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

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.

如何强制选择为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);
}

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

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

推荐答案

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

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

这篇关于使用WCF,LINQ,JSON时无法序列化类型'System.Linq.Enumerable ...'的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆