Problem using ReadAsDataContract using WCF REST Toolkit
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()开发者_JAVA技巧> _
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.
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 the XmlSerializer
to deserialize that. If you include (Import) the namespace System.Xml.Serialization
, you should get the extension methods ReadAsXmlSerializable
, which is what you should be using to deserialize the response.
精彩评论