2

我正在尝试托管一个通过提供 json 输出流来响应传入请求的 WCF 服务。我有以下类型

[DataContract]  
[KnownType(typeof(List<HubCommon>))]
[KnownType(typeof(Music))]
[KnownType(typeof(AppsAndPlugins))]
[KnownType(typeof(Notifications))]
[KnownType(typeof(Scenes))]
[KnownType(typeof(Skins))]
[KnownType(typeof(Ringtones))]
[KnownType(typeof(Alarms))]
[KnownType(typeof(Widgets))]
[KnownType(typeof(Wallpapers))]
[KnownType(typeof(Soundsets))]
public class HubCommon{}

在我的 *.svc.cs 文件中,我执行以下操作

List<HubCommon> hubContent = _ldapFacade.GetResults(query);
        MemoryStream stream = new MemoryStream();
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(HubCommon));          
        serializer.WriteObject(stream,hubContent);

所以本质上我正在尝试将列表序列化为 Json,但在“WriteObject”执行时出现以下错误:-

服务器在处理请求时遇到错误。异常消息是 'Type 'System.Collections.Generic.List`1[[HubContentCore.Domain.HubCommon, HubContentCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]',数据合约名称为 'ArrayOfHubCommon:http ://schemas.datacontract.org/2004/07/HubContentCore.Domain' 不是预期的。将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。

我在这里想念什么?

提前致谢。

4

1 回答 1

1

您的 DataContractJsonSerializer 的类型是,HubCommon但您正在编写一个类型的对象List<HubCommon>并且HubCommon未添加到KnownTypAttribute

于 2010-10-05T00:57:52.247 回答