WCF WebInvoke 响应格式

发布于 2024-07-24 08:57:35 字数 1304 浏览 5 评论 0原文

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

    [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 并将格式作为方法的参数? 通过阅读周围的资料,我不这么认为......好吧,接下来的事情 上面的代码没问题并且可以工作,但是我有一个问题,我希望能够指定默认值,所以当没有传递格式争论时,我只是默认值,但是如果我像这样装饰

    [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是默认值,如果我尝试通过浏览器调用服务方法,它告诉我:

UriTemplateTable 不支持具有与模板“getstreamurl?ch={ch}”等效的路径但具有不同查询字符串的多个模板,其中查询字符串无法全部通过文字值消除歧义。 有关更多详细信息,请参阅 UriTemplateTable 的文档

它们显然可以区分,但似乎 WCF 仅读取参数,仅此而已...有什么建议吗?

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")]

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")]

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作业与我同在 2024-07-31 08:57:35

不,我认为您不能在运行时以编程方式执行此操作。 当然,您可以做什么,如果从您的服务公开两个不同的端点 - 一个返回 XML,另一个返回 JSON,然后以编程方式选择从客户端应用程序调用哪个端点。

Marc

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

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

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.

Marc

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.

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

荒岛晴空 2024-07-31 08:57:35

如果您的休息服务配置为自动选择响应类型,您可以执行此操作。

然后在客户端请求时只需添加所需的标头 Accept: application/json

You can do this if your rest service is configured automatically select response type.

Then on client request simply add needed header Accept: application/json

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文