我通過NuGet(pkg版本0.3.0)使用WCF WebApi堆棧(預覽版4),並且似乎無法弄清楚如何使用HttpClient
「發佈POCO」。如何使用WCF發佈POCO WebApi的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
,但是沒有幫助。我只是做錯了什麼?
我發現這裏this post在計算器上它看起來像一個人是做類似於我上面做了。我甚至已經重複他的工作(猜他Machine
對象是一個簡單的POCO像我MyInfo
是),並遇到同樣的「無法序列」異常。
而是在爲「text/xml」的硬編碼最後我用XmlMediaTypeFormatter.DefaultMediaType.MediaType(這是「應用程序/ XML」 - 「更好的」媒體類型無論如何)。仍然想知道我是否可以避免這樣做,只是回退一些理智的默認值。 – ckittel