2011-07-27 75 views
5

我通過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是),並遇到同樣的「無法序列」異常。

回答

5

我最終找到自己的問題。我錯過了媒體類型。我改變了Post方法是這樣的:

Using client as New HttpClient(baseUri) 
    Using response = client.Post(requestUri, New ObjectContent(Of MyInfo)(value, "text/xml")) 
     ' Do stuff 
    End Using 
End Using 

,並開始工作。我想這是有道理的。出於某種原因,我認爲有某種內置的默認媒體類型優先順序,這樣您就不必每次都不用指定媒體類型。也許有,但我仍然做錯了什麼?

更新:

我原來的問題,實際上一點關係都沒有與媒體類型,即使實際上沒有修復/解決辦法的問題。這個問題似乎在嵌套在Module這樣的內部MyInfo發生。

Module Module1 

    Sub Main() 
     Dim value = New MyInfo With {.MyDate = Today, .MyId = Guid.NewGuid()} 
     Dim payload As HttpContent = New ObjectContent(Of MyInfo)(value) 

     Using client as New HttpClient(baseUri) 
      Using response = client.Post(requestUri, New ObjectContent(Of MyInfo)(value)) 
       ' Do stuff 
      End Using 
     End Using 
    End Sub 

    Public Class MyInfo 
     Public Property MyDate As DateTime 
     Public Property MyId As Guid 
    End Class 

End Module 

一旦MyInfo移動Module之外,錯誤不再發生。另外值得一提的是,當媒體類型不再需要防止異常,缺乏則導致無Content-Type頭,這可能不是飛服務器端的服務器可能不知道如何反序列化消息體。在我的情況下,我需要離開媒體類型,以便請求包含服務器的Content-Type: application/xml標頭。

+0

而是在爲「text/xml」的硬編碼最後我用XmlMediaTypeFormatter.DefaultMediaType.MediaType(這是「應用程序/ XML」 - 「更好的」媒體類型無論如何)。仍然想知道我是否可以避免這樣做,只是回退一些理智的默認值。 – ckittel