我正在通过 NuGet(pkg 版本 0.3.0)使用 WCF WebApi 堆栈(预览版 4),并且似乎无法弄清楚如何使用 HttpClient
“发布 POCO” .
鉴于以下情况:
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在 StackOverflow 上,看起来有人正在做与我上面所做的类似的事情。我什至重复了他的工作(猜测他的 Machine
对象是一个简单的 POCO,就像我的 MyInfo
一样)并遇到了相同的“无法序列化”异常。
最佳答案
我最终自己发现了问题。我缺少媒体类型。我将 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
它开始工作了。我想这是有道理的。出于某种原因,我认为内置了某种默认的媒体类型优先级,这样您就不必每次都特意指定媒体类型。也许有,但我仍然做错了什么?
更新:
我原来的问题实际上与媒体类型无关,尽管这实际上确实解决/解决了问题。当 MyInfo
嵌套在 Module
中时,似乎会发生问题。
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
header ,这可能无法在服务器端飞行,因为服务器可能不知道如何反序列化消息正文。就我而言,为了服务器的缘故,我需要保留媒体类型,以便请求包含 Content-Type: application/xml
header 。
关于wcf - 如何使用 WCF WebApi 的 HttpClient 发布 POCO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6849604/