WCF Web API 预览 6:无“MediaTypeFormatter”可用
我正在考虑在 WCF Web API Preview 6 中使用 HttpClient 来使用第三方服务。该第三方服务接受并返回 XML 格式的数据。它们的 HTTP 响应的 Content-Type 标头设置为 text/plain。看来将响应 Content-Type 设置为 text/plain 会导致问题。我按如下方式向服务发出请求:
Task<HttpResponseMessage> result = client.PostAsync(apiEndpoint, new ObjectContent(typeof (LeaveAccrualRequest), request));
使用 Fiddler,我可以看到请求发送到服务,并返回适当的预期响应。但是,当我尝试访问响应时,最终出现以下 InvalidOperationException:
没有“MediaTypeFormatter”可用于读取媒体类型为“text/plain”的“LeaveAccrualResponse”类型的对象。
是有没有办法告诉 HttpClient,即使 HTTP 响应说内容类型是 text/plain,它也应该将其处理为 application/xml?
I'm looking at using HttpClient in WCF Web API Preview 6 to consume a third party service. This third party service accepts and returns XML formatted data. Their HTTP responses have the Content-Type header set to text/plain. It appears that having the response Content-Type set to text/plain is causing problems. I'm making the request to the service as follows:
Task<HttpResponseMessage> result = client.PostAsync(apiEndpoint, new ObjectContent(typeof (LeaveAccrualRequest), request));
Using Fiddler, I can see the request go to the service and an appropriate, expected response come back. However when I try to access the response, I end up with the following InvalidOperationException:
No 'MediaTypeFormatter' is available to read an object of type 'LeaveAccrualResponse' with the media type 'text/plain'.
Is there a way to tell HttpClient that even though the HTTP response says the content type is text/plain, it should handle it as application/xml?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从 XmlMediaTypeFormatter 派生并添加“text/plain”标头:
根据您的要求,在添加“text/plain”之前删除所有其他支持的媒体类型可能是有意义的:
[更新]
访问您的请求内容并使用 ReadAsAsync; 接受 IEnumerable的方法覆盖。
You could derive from XmlMediaTypeFormatter and add your "text/plain" header:
Depending on your requirements it could make sense to remove all other supported media types before adding "text/plain:
[Update]
Access your requests Content and use the ReadAsAsync<T> method overlaod which accepts an IEnumerable<MediaTypeFormatter>.