简体   繁体   English

WCF Web API,Hammock和WP7 Woes

[英]WCF Web API, Hammock and WP7 Woes

Facing an interesting issue when I am using WCF Web API with Hammock and WP7. 当我使用带有Hammock和WP7的WCF Web API时遇到一个有趣的问题。 As you know, WCF Web API allows you to change the response type based on what you specify in the Accept header. 如您所知,WCF Web API允许您根据在Accept标头中指定的内容更改响应类型。 For eg if you send Accept:application/json, the response is JSON, if you specify application/xml you get XML and so on. 例如,如果发送Accept:application / json,则响应为JSON,如果指定application / xml,则获取XML等。

So I created my Web API which works perfectly fine from a Web Browser. 所以我创建了我的Web API,它可以从Web浏览器中完美地运行。 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. 但是,当我在WP7中使用它时,接受头是什么并不重要,看起来WP7将它替换为/并且我只返回XML。

I know for a fact that it isn't Hammock that is the issue because I tried with Facebook Graph API. 我知道这个问题不是Hammock,因为我尝试使用Facebook Graph API。 If I change the Accept to say: application/hello , I get a bad request error. 如果我将Accept更改为: application / hello ,则会收到错误的请求错误。 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. 但是,当我使用C#代码中的WP7执行此操作时,我绝对没有错误,返回类型是XML,无论我发送什么。

To circumvent this issue, I created a JSON.Net Serializer in my WCF Web API. 为了解决这个问题,我在WCF Web API中创建了一个JSON.Net Serializer。 Everything worked great until I started using HttpResponseMessage in my methods as return types. 一切都运行良好,直到我开始在我的方法中使用HttpResponseMessage作为返回类型。 Now even though i have a JSON serializer, I get XML for all methods where I use the HttpResponseMessage. 现在即使我有一个JSON序列化程序,我获得了使用HttpResponseMessage的所有方法的XML。

As you can see, I am in lala land now. 如你所见,我现在在lala land。

Can anyone provide any feedback? 任何人都可以提供反馈吗?

Any help is highly appreciated. 任何帮助都非常感谢。

I ran into this and posted this issue on CodePlex. 我遇到了这个并在CodePlex上发布了这个问题 It's been fixed if you pull the latest source, although Nuget hasn't been updated yet. 虽然Nuget还没有更新,但是如果您提取最新的来源已得到修复。 You should then be able to remove the xml formatter or clear the built in formatters and add the one you want. 然后,您应该能够删除xml格式化程序或清除内置格式化程序并添加所需的格式化程序。 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. 更新:现在已经在nuget上使用WCF Web API Preview 6修复了此问题。

The WCF WebApi lets you specify the format using the querystring if you want to. 如果您愿意,WCF WebApi允许您使用查询字符串指定格式。 You need to update the configuration with QueryStringMapping in addition to the RequestHeaderMapping that is there by default. 除了默认情况下存在的RequestHeaderMapping之外,您还需要使用QueryStringMapping更新配置。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    var config = routes.GetDefaultHttpConfiguration();

    var jsonFormatters = config.Formatters.Where(f => f.SupportedMediaTypes.Any(mt => mt.MediaType == "application/json"));
    jsonFormatters.ToList().ForEach(f => f.MediaTypeMappings.Add(new QueryStringMapping("format", "json", "application/json")));

    routes.MapServiceRoute<BooksService>("books", config);

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM