【发布时间】: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<MyObject>而不是一个MyObject- 否则你能不能包含执行序列化的代码。序列化一个Task<T>总是会失败,因为它包含一个Exception,而Dictionary又包含一个不可序列化的Dictionary。 -
谢谢你,我正试图找出原因..顺便说一句,我想知道为什么这样做的人不赞成:)
-
能否包含序列化对象的代码。我们很有可能会在那里找到问题。
标签: c# .net wcf serialization