我正在测试Json.Net的最新版本,我正在序列化服务器上的集合并使用 WCF 代理传递字符串。
服务器调用需要不到一秒的时间来返回数据,但在 PDA 客户端,反序列化JsonConvert.DeserializeObject(Of IList(Of BusinessUnitModel))(results)
需要 20-30 秒。
这种性能在 Json.Net 中很常见,还是我对对象或序列化/反序列化做错了什么?
我曾尝试降级到 Json.Net 的 3.5x 版本,并在服务器上使用适当的版本,在客户端使用 Compact 版本,但性能再次很差。
共享模型
Imports System.ComponentModel.DataAnnotations
Namespace Models
Public Class BusinessUnitModel
<Required>
Public Property ID As Int32
<Required>
<StringLength(50)>
Public Property Name As String
<Required>
Public Property IsInternalSupplier As Boolean
End Class
End Namespace
服务器端序列化
Imports Qsmart.DataProvider.Contracts
Imports System.ComponentModel.Composition
Imports Newtonsoft.Json
Namespace Serialiser
<Export(GetType(IModelSerialiser))>
Public Class JSonSerialiser
Implements IModelSerialiser
<Import(GetType(ILoggerHelper))>
Private Property ExHelper As ILoggerHelper
Friend Function Deserailse(Of TModel)(serialised As String) As TModel Implements IModelSerialiser.Deserailse
If serialised Is Nothing Then Throw New ArgumentNullException("serialised")
Try
Return JsonConvert.DeserializeObject(serialised)
Catch ex As JsonSerializationException
ExHelper.Process(ex)
Throw New ApplicationException("Could Not Deserialse The Model", ex)
End Try
End Function
Friend Function Serailse(Of TModel)(model As TModel) As String Implements IModelSerialiser.Serailse
If model Is Nothing Then Throw New ArgumentNullException("model")
Try
Return JsonConvert.SerializeObject(model)
Catch ex As JsonSerializationException
ExHelper.Process(ex)
Throw New ApplicationException("Could Not Serialse The Model", ex)
End Try
End Function
End Class
End Namespace
服务器端测试数据创建
Public Function QSmartBusinessUnits() As String Implements IQSmartDataProvider.QSmartBusinessUnits
Dim serialsedResult As String = String.Empty
Try
Dim tester As New List(Of BusinessUnitModel)
tester.Add(New BusinessUnitModel With {.ID = 1, .Name = "Test1", .IsInternalSupplier = True})
tester.Add(New BusinessUnitModel With {.ID = 2, .Name = "Test2", .IsInternalSupplier = True})
tester.Add(New BusinessUnitModel With {.ID = 3, .Name = "Test3", .IsInternalSupplier = True})
tester.Add(New BusinessUnitModel With {.ID = 4, .Name = "Test4", .IsInternalSupplier = True})
tester.Add(New BusinessUnitModel With {.ID = 5, .Name = "Test5", .IsInternalSupplier = True})
tester.Add(New BusinessUnitModel With {.ID = 6, .Name = "Test6", .IsInternalSupplier = True})
serialsedResult = Serialiser.Serailse(tester)
Catch ex As Exception
ExHelper.Process(ex)
End Try
Return serialsedResult
End Function
紧凑框架反序列化
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Try
Dim results As String = _proxy.QSmartBusinessUnits()
Dim deserialise = JsonConvert.DeserializeObject(Of IList(Of BusinessUnitModel))(results)
BusinessUnitModelBindingSource.DataSource = deserialise
Catch ex As Exception
Stop
End Try
End Sub
更新
如果我从模型中删除 DataAnnotations,性能会好得多,它会下降到大约 2 秒......知道为什么会这样吗?
Imports System.ComponentModel.DataAnnotations
Namespace Models
Public Class BusinessUnitModel
Public Property ID As Int32
Public Property Name As String
Public Property IsInternalSupplier As Boolean
End Class
End Namespace