我正在通过 NuGet(pkg 版本 0.3.0)使用 WCF WebApi 堆栈(预览版 4),但似乎无法弄清楚如何使用HttpClient
.
鉴于以下情况:
Public Class MyInfo
Public Property MyDate As DateTime
Public Property MyId As Guid
End Class
...
Dim value = New MyInfo With {.MyDate = Today, .MyId = Guid.NewGuid()}
Using client as New HttpClient(baseUri)
Using response = client.Post(requestUri, New ObjectContent(Of MyInfo)(value))
' Do stuff
End Using
End Using
...
调用该Post
方法时,出现以下异常:
The 'XmlSerializer' serializer cannot serialize the type 'MyInfo'.
at Microsoft.ApplicationServer.Http.XmlMediaTypeFormatter.GetSerializerForType(Type type)
at Microsoft.ApplicationServer.Http.XmlMediaTypeFormatter.OnWriteToStream(Type type, Object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext context)
at Microsoft.ApplicationServer.Http.MediaTypeFormatter.WriteToStream(Type type, Object instance, Stream stream, HttpContentHeaders contentHeaders, TransportContext context)
at Microsoft.ApplicationServer.Http.ObjectContent.WriteToStreamInternal(Stream stream, TransportContext context)
at Microsoft.ApplicationServer.Http.ObjectContent.SerializeToStream(Stream stream, TransportContext context)
at System.Net.Http.HttpContent.LoadIntoBuffer(Int32 maxBufferSize)
at System.Net.Http.HttpClientChannel.PrepareWebRequestForContentUpload(HttpWebRequest webRequest, HttpRequestMessage request)
at System.Net.Http.HttpClientChannel.CreateAndPrepareWebRequest(HttpRequestMessage request)
at System.Net.Http.HttpClientChannel.Send(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.Send(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.Send(HttpRequestMessage request)
at System.Net.Http.HttpClient.Post(Uri requestUri, HttpContent content)
at System.Net.Http.HttpClient.Post(String requestUri, HttpContent content)
...
这是使用 NuGet 0.3.0 包。
我试过添加<Serializable()>
,甚至<DataContract()>
添加MyInfo
,但这没有帮助。我只是做错了什么吗?
我在 StackOverflow 上找到了这篇文章,看起来有人正在做类似于我上面所做的事情。我什至复制了他的工作(猜测他的Machine
对象是一个像我一样的简单 POCO MyInfo
)并遇到了相同的“无法序列化”异常。