【问题标题】:Object not serializable on Windows XP对象在 Windows XP 上不可序列化
【发布时间】:2014-11-26 16:45:08
【问题描述】:

我使用 C# 和 WCF 制作了客户端和服务。它们在我可以尝试的所有现代操作系统上完美运行,包括 x86 和 x64。

现在,在 Windows XP 上尝试它时,由于这个错误,它不起作用:

无法序列化类型“System.Threading.Tasks.Task`1[MyObject[]]”。 考虑使用 DataContractAttribute 属性对其进行标记,并且 标记您想要序列化的所有成员 DataMemberAttribute 属性

在我的服务接口上我使用了这个

[OperationContract()]
List<MyObject> GetFileList(string randomString, string uniqueID);

MyObject 看起来像这样

[Serializable()]
public class MyObject
{
    public string oneRandomWorld { get; set; }
    public string helloImAVariable { get; set; }

    public SingleVMFileInfo(string oneRandomWorld, string helloImAVariable)
    {
        this.oneRandomWorld = oneRandomWorld;
        this.helloImAVariable = helloImAVariable;
    }
}

里面只包含字符串。我尝试用这两种方法扩展MyObject

//Deserializer
public MyObject(SerializationInfo info, StreamingContext ctxt)
{
    oneRandomWorld = (string)info.GetValue("oneRandomWorld", typeof(string));
    helloImAVariable = (string)info.GetValue("helloImAVariable", typeof(string));
}

//Serializer
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
    info.AddValue("oneRandomWorld", oneRandomWorld);
    info.AddValue("helloImAVariable", helloImAVariable);
}

没有任何不同的结果。 这适用于 Windows 7、8、8.1、Server 2008R2、Server 2012、Server 2012 R2.. 但在 Windows XP 和 Windows Server 2003 上会出现该错误。

我有点缺乏想法,我可以尝试什么?

【问题讨论】:

  • 你有什么理论可以解释为什么它试图序列化一个System.Threading.Tasks.Task&lt;MyObject&gt; 而不是一个MyObject - 否则你能不能包含执行序列化的代码。序列化一个Task&lt;T&gt; 总是会失败,因为它包含一个Exception,而Dictionary 又包含一个不可序列化的Dictionary
  • 谢谢你,我正试图找出原因..顺便说一句,我想知道为什么这样做的人不赞成:)
  • 能否包含序列化对象的代码。我们很有可能会在那里找到问题。

标签: c# .net wcf serialization


【解决方案1】:

我终于解决了这个问题,编辑了我的generatedProxy.cs,我用svcutil.exe创建的那个

对于每个函数,该文件都包含 syncasync 版本。即使我从未使用过任何异步版本,程序仍会尝试初始化它并导致失败。

删除所有 aSync 版本解决了我的问题。

我仍然不明白为什么只有 Windows XP 和 Windows Server 2003 不喜欢这个,但我就是这样做的。

感谢 Dead.Rabit 提供的信息,没有它我无法想象问题!

ps:我仍然不明白为什么我投了反对票.. 好吧,我得忍受它! :)

【讨论】:

    猜你喜欢
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多