【问题标题】:Using DataContractJsonSerializer使用 DataContractJsonSerializer
【发布时间】:2010-10-05 00:41:14
【问题描述】:

我正在尝试托管一个通过提供 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);

所以本质上我正在尝试将 List 序列化为 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 的已知类型列表中。'

我在这里缺少什么?

提前致谢。

【问题讨论】:

    标签: json wcf datacontractjsonserializer


    【解决方案1】:

    您的 DataContractJsonSerializer 的类型是 HubCommon,但您正在编写 List&lt;HubCommon&gt; 类型的对象,并且 HubCommon 未添加到 KnownTypAttribute

    【讨论】:

    • 谢谢!! Wayyy 盯着屏幕看了一天……完全错过了。真的很感激。
    • :D 非常感谢 Rob 帮助你不再整天盯着屏幕看,哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    相关资源
    最近更新 更多