WCF Web API、Hammock 和 WP7 问题
当我将 WCF Web API 与 Hammock 和 WP7 一起使用时,遇到一个有趣的问题。如您所知,WCF Web API 允许您根据 Accept 标头中指定的内容更改响应类型。例如,如果您发送 Accept:application/json,则响应为 JSON,如果您指定 application/xml,您将获得 XML 等等。
所以我创建了我的 Web API,它在 Web 浏览器中运行得非常好。正如预期的那样,我得到了我想要的类型。然而,当我在 WP7 中使用它时,Accept 标头是什么并不重要,看起来 WP7 将其替换为 / 并且我只返回 XML。
我知道问题不是 Hammock,因为我尝试使用 Facebook Graph API。如果我将接受更改为:application/hello,我会收到错误请求错误。但是,当我在 C# 代码中使用 WP7 执行此操作时,绝对不会出现任何错误,并且无论我发送什么内容,返回类型都是 XML。
为了避免这个问题,我在 WCF Web API 中创建了一个 JSON.Net 序列化器。一切都很顺利,直到我开始在我的方法中使用 HttpResponseMessage 作为返回类型。现在,即使我有一个 JSON 序列化器,我也会获得使用 HttpResponseMessage 的所有方法的 XML。
正如你所看到的,我现在在啦啦乐园。
有人可以提供任何反馈吗?
非常感谢任何帮助。
Facing an interesting issue when I am using WCF Web API with Hammock and WP7. As you know, WCF Web API allows you to change the response type based on what you specify in the Accept header. For e.g. if you send Accept:application/json, the response is JSON, if you specify application/xml you get XML and so on.
So I created my Web API which works perfectly fine from a Web Browser. As expected I get the types I want. Hwoever, when I use this from WP7, it doesn't matter what the Accept header is, it looks like WP7 replaces it to / and I only get back XML.
I know for a fact that it isn't Hammock that is the issue because I tried with Facebook Graph API. If I change the Accept to say: application/hello, I get a bad request error. However, when I do that with WP7 in C# code, I get absolutely no errors and the return type is XML irrespective of what I send.
To circumvent this issue, I created a JSON.Net Serializer in my WCF Web API. Everything worked great until I started using HttpResponseMessage in my methods as return types. Now even though i have a JSON serializer, I get XML for all methods where I use the HttpResponseMessage.
As you can see, I am in lala land now.
Can anyone provide any feedback?
Any help is highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了这个问题,并在 CodePlex 上发布了此问题。尽管 Nuget 尚未更新,但如果您提取最新的源代码,该问题已得到修复。然后,您应该能够删除 xml 格式化程序或清除内置格式化程序并添加您想要的格式化程序。我发现的另一个解决方法是清除内置格式化程序并创建映射到 / 接受类型的自定义媒体类型格式化程序。
更新:现在 nuget 上的 WCF Web API Preview 6 已修复此问题。
I ran into this and posted this issue on CodePlex. It's been fixed if you pull the latest source, although Nuget hasn't been updated yet. You should then be able to remove the xml formatter or clear the built in formatters and add the one you want. Another workaround that I found was to clear the built in formatters and create a custom media type formatter that mapped to / accept type.
Update: this has been fixed with WCF Web API Preview 6 now on nuget.
如果需要,WCF WebApi 允许您使用查询字符串指定格式。除了默认情况下的 RequestHeaderMapping 之外,您还需要使用 QueryStringMapping 更新配置。
The WCF WebApi lets you specify the format using the querystring if you want to. You need to update the configuration with QueryStringMapping in addition to the RequestHeaderMapping that is there by default.