使用 WCF REST 工具包使用 ReadAsDataContract 时出现问题
我是序列化方面的新手,我遇到了问题。我有一个返回 IDDescriptionPair 对象数组的 REST 服务。使用该服务时,我使用“将 XML 作为类型粘贴”VS 加载项来创建对象。我只是修改此对象以添加 DataContract 属性,以便我的命名空间在两端匹配。这是该对象:
Imports System.Runtime.Serialization
<DataContract([Name]:="IDDescriptionPair", [Namespace]:="http://schemas.datacontract.org/2004/07/Blizzard.ClassLibrary")>
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225"), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://schemas.datacontract.org/2004/07/Blizzard.ClassLibrary"), _
System.Xml.Serialization.XmlRootAttribute ([Namespace]:="http://schemas.datacontract.org/2004/07/Blizzard.ClassLibrary", IsNullable:=True)> _
Partial Public Class IDDescriptionPair
Private descriptionField As String
Private idField As Integer
Private idFieldSpecified As Boolean
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute(IsNullable:=True)> _
Public Property Description() As String
Get
Return Me.descriptionField
End Get
Set(value As String)
Me.descriptionField = value
End Set
End Property
'''<remarks/>
Public Property ID() As Integer
Get
Return Me.idField
End Get
Set(value As Integer)
Me.idField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public Property IDSpecified() As Boolean
Get
Return Me.idFieldSpecified
End Get
Set(value As Boolean)
Me.idFieldSpecified = value
End Set
End Property
End Class
我可以调用该服务并反序列化该对象,并且它看起来工作正常。我得到了正确数量的 IDDescriptionPair 对象的列表。问题是它们都是空白的 - 没有填充任何属性。
这是我使用该服务的代码:
Dim client As New HttpClient()
Dim endpoint As New Uri("http://bmpscnt410a/services/v1/personservices/offices/5/principals")
Using response As HttpResponseMessage = client.Get(endpoint)
response.EnsureStatusIsSuccessful()
Dim idp As List(Of IDDescriptionPair)
Try
idp = response.Content.ReadAsDataContract(Of List(Of IDDescriptionPair))()
Catch ex As Exception
End Try
End Using
我尝试直接使用 DataContractSerializer,但得到了相同的结果(我猜这是预期的)。任何想法将不胜感激。
I'm a novice at serialization and I've run into a problem. I have a REST service that returns an array of IDDescriptionPair objects. When consuming the service, I'm using the "Paste XML as Types" VS add-in to create an object. I'm only modifying this object to add the DataContract attribute so my namespaces match on each end. Here's that object:
Imports System.Runtime.Serialization
<DataContract([Name]:="IDDescriptionPair", [Namespace]:="http://schemas.datacontract.org/2004/07/Blizzard.ClassLibrary")>
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225"), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://schemas.datacontract.org/2004/07/Blizzard.ClassLibrary"), _
System.Xml.Serialization.XmlRootAttribute ([Namespace]:="http://schemas.datacontract.org/2004/07/Blizzard.ClassLibrary", IsNullable:=True)> _
Partial Public Class IDDescriptionPair
Private descriptionField As String
Private idField As Integer
Private idFieldSpecified As Boolean
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute(IsNullable:=True)> _
Public Property Description() As String
Get
Return Me.descriptionField
End Get
Set(value As String)
Me.descriptionField = value
End Set
End Property
'''<remarks/>
Public Property ID() As Integer
Get
Return Me.idField
End Get
Set(value As Integer)
Me.idField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public Property IDSpecified() As Boolean
Get
Return Me.idFieldSpecified
End Get
Set(value As Boolean)
Me.idFieldSpecified = value
End Set
End Property
End Class
I can call the service and deserialize the object, and it appears to work fine. I get a list of the correct number of IDDescriptionPair objects. The problem is that they're all blank - none of the properties are populated.
Here's the code where I consume the service:
Dim client As New HttpClient()
Dim endpoint As New Uri("http://bmpscnt410a/services/v1/personservices/offices/5/principals")
Using response As HttpResponseMessage = client.Get(endpoint)
response.EnsureStatusIsSuccessful()
Dim idp As List(Of IDDescriptionPair)
Try
idp = response.Content.ReadAsDataContract(Of List(Of IDDescriptionPair))()
Catch ex As Exception
End Try
End Using
I've tried using DataContractSerializer directly, but I get the same result (which is expected, I guess). Any ideas would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的类型是 XmlSerializer 类型(使用
System.Xml.Serialization
命名空间中的属性进行注释,例如
、< ;XmlRoot()>
等等)。为此,您需要使用XmlSerializer
对其进行反序列化。如果包含(导入)命名空间System.Xml.Serialization
,您应该获得扩展方法ReadAsXmlSerialized
,这是您应该用来反序列化响应的方法。The type which you use is a XmlSerializer type (annotated with attributes from the
System.Xml.Serialization
namespace, such as<XmlType()>
,<XmlRoot()>
and so on). For that you'll need to use theXmlSerializer
to deserialize that. If you include (Import) the namespaceSystem.Xml.Serialization
, you should get the extension methodsReadAsXmlSerializable
, which is what you should be using to deserialize the response.