WCF Web调用ResponseFormat [英] WCF WebInvoke ResponseFormat

查看:122
本文介绍了WCF Web调用ResponseFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WCF restul服务,我想允许用户选择他们想要的请求格式,我有装饰物

I have a WCF restul service and I want to allow the user to choose what request format they want, i have the decorations

    [OperationContract]
    [WebInvoke(Method = "GET", 
        ResponseFormat = WebMessageFormat.Xml, 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        UriTemplate = "getstreamurl?ch={ch}&format=xml")]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "getstreamurl?ch={ch}&format=json")]

首先,有没有一种方法可以在运行时指定ResponseFormat并将格式作为方法的参数?从阅读周围我不这么认为...好吧下一件事 上面的代码是可以正常工作的,但是我有一个问题,我希望能够指定一个默认值,所以当没有通过格式争论时,我只是默认值,但是如果我这样装饰

First of, is there a way to specify the ResponseFormat at runtime and take the format in as an argument to the method? From reading around i dont think so... OK next thing The above code is ok and works, but im having a problem, i want to be able to specify a default, so when no format arguement is passed then i just default but if i decorate like so

    [WebInvoke(Method = "GET", 
        ResponseFormat = WebMessageFormat.Xml, 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        UriTemplate = "getstreamurl?ch={ch})]

    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "getstreamurl?ch={ch}&format=json")]

XML是默认值,如果我尝试通过浏览器调用service方法,它将告诉我:

Where the XML is the default, if i try to call the service method through the browser it tells me that:

UriTemplateTable不支持与模板'getstreamurl?ch = {ch}'具有相同路径的多个模板,但是具有不同的查询字符串,这些查询字符串不能全部通过文字值消除歧义.有关更多详细信息,请参见UriTemplateTable的文档

UriTemplateTable does not support multiple templates that have equivalent path as template 'getstreamurl?ch={ch}' but have different query strings, where the query strings cannot all be disambiguated via literal values. See the documentation for UriTemplateTable for more detail

显然可以区分它们,但似乎WCF只是在阅读论点,就这样...有什么建议吗?

They obviously can be distinguished but it seems that WCF is only reading up to the argument and thats it...Any suggestions?

推荐答案

不,我不认为您可以在运行时以编程方式进行操作.如果要从服务中公开两个截然不同的端点-一个返回XML,另一个返回JSON,然后以编程方式从客户端应用程序中选择要调用的那个端点,您可以做的当然.

No, I don't think you can do that programmatically at runtime. What you can do of course if to expose two distinct endpoints from your service - one returning XML, another returning JSON, and then programmatically pick which one to call from your client app.

马克

更新:正如Steve Michelotti正确指出的那样,现在可以在WCF 4.0中实现JSON和XML之间的这种自动切换. WCF 4.0改进了REST支持,其中还包括基于HTTP接受标头的格式消息选择"功能.

Update: as Steve Michelotti correctly points out, this automatic switching between JSON and XML can now be achieved in WCF 4.0. WCF 4.0 has an improved REST support which also includes an Format Message Selection feature, based on HTTP accept headers.

有关WCF 4.0的新功能的详细信息,请参阅:开发人员的WCF 4.0简介

For more info on WCF 4.0's new features, see: A Developer's Introduction to WCF 4.0

这篇关于WCF Web调用ResponseFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆