无法反序列化wcf类对象

时间:2017-04-02 08:40:35

标签: .net json winforms wcf serialization

我有一个带WSHttpBinding的WCF服务。客户端和服务器都在.net平台上。在Windows窗体应用程序中,我调用一个函数来获取任务并将该任务保存在文件中,稍后,将该任务加载为:

DataContract()>
Public Class Task

    <DataMember()>
    Public Property UID As String

    <DataMember()>
    Public Property Name As String

    <DataMember()>
    Public Property progress As String 

    <DataMember()>
    Public Property Type As Long
    <DataMember()>
    Public Property Version As String  Version

    <DataMember()>
        Public Property Status As Long
    <DataMember()>
        Public Property Narr As String
    <DataMember()>
        Public Property Dt As Date
End Class

我正在调用一个序列化对象并保存到文件的函数,以便稍后可以轻松地将其加载到对象:

Function GetSaveTask()
   Dim Task_ As serv.Task = ServObj.GetTask() '' Calling Service to get Task
   Dim ObStrng As String = Jserialize(Task_)
   File.WriteAllText("D:\Task.ini", ObStrng)
End Function
Function LoadTask()
   Dim Task_ As serv.Task
   Dim ObStrng As String = File.ReadAllText("D:\Task.ini")
   Task_ = Deserialize(Of serv.Task)(ObStrng)

End Function
Public Shared Function Jserialize(Of T)(ByVal obb As Object) As Object
    Dim serializer As New Web.Script.Serialization.JavaScriptSerializer()
    Return serializer.Serialize(obb)
End Function
 Public Function Deserialize(Of T)(ByVal obb As String) As Object
    Dim JavaScriptSerializer As New Web.Script.Serialization.JavaScriptSerializer()
    Return JavaScriptSerializer.Deserialize(Of T)(obb)
End Function

Task.ini中的序列化数据:

[{"ExtensionData":{},"Dt":"\/Date(1490985000000)\/","Name":"Task-2017",
"Narr":null,"Status":20,"Type":0,"UID":"ABBB-KKKK","Version":"1.0.1.2","progress":20}]

我只能使用System.Web.Script.Serialization.JavaScriptSerializer。 Task_可以轻松地序列化并保存到File。但是LoadTask()无法反序列化并抛出错误:

  

没有为'System.Runtime.Serialization.ExtensionDataObject'的类型定义无参数构造函数

我尝试创建与Class Task 相同的代理类,并将其保存到文件中。但该服务不断更新DataMember。我们只是更新服务参考,一切正常。因此代理类将是一个开销,并且必须在服务类更改时继续更新(这在整个项目中是不可能的)。

为什么无法反序列化?有没有办法加载所有,除了ExtensionData?

1 个答案:

答案 0 :(得分:1)

ExtensionData导致该错误。 ExtensionData实际上是必须内置到类型中以启用往返的功能。它始终由DataContractSerializer发出。正如您所说,服务不断更新,您可以做的一件事是,每次更新服务引用时,都要评论ExtensionData及其实现。

 <System.NonSerializedAttribute()>  _
 Private extensionDataField As System.Runtime.Serialization.ExtensionDataObject

并删除Implements

Implements System.Runtime.Serialization.IExtensibleDataObject