WCF 服务中的 JSONPBehaviour 和多种格式

发布于 2024-08-30 11:51:06 字数 1198 浏览 2 评论 0原文

我有一个 WCF 服务,我想根据 URI 模板生成 XML 和 JSON。因此,在我的服务合同中,我有如下方法,

[ServiceContract]
public interface MultiFormatContract
{
  [OperationContract]
  [WebGet(UriTemplate="/json/data", ResponseFormat = WebMessageFormat.Json)]
  [JSONPBehavior(callback = "callback")]
  public MySerializableObject GetJSONData()


  [OperationContract]
  [WebGet(UriTemplate = "/xml/data", ResponseFormat=WebMessageFormat.Xml)]
  public MySerializableObject GetXMLData()
}

我需要支持 JSONP 并使用 Microsoft REST 示例来支持此操作(遵循此 http://jasonkelly.net/archive/2009/02/24/using -jquery-amp-jsonp-for-cross-domain-ajax-with-wcf-services.aspx)。然而,这意味着我必须添加自定义绑定:

  <customBinding>
    <binding name="jsonpBinding">
      <jsonpMessageEncoding/>
      <httpTransport manualAddressing="true"/>
    </binding>
  </customBinding>

这意味着 WCF 不再使用 textMessageEncoding,即使 JSONPEncoder 覆盖文本编码器。我的端点无法再生成 Xml。

我不想将消费者锁定到 JSON,并且希望能够浏览到我的 Xml 端点以查看用于调试的 Xml 等。

关于如何同时拥有这两者,有什么想法吗?

I have a WCF service which I would like to product XML and JSON depending on the URI template. So in my service contract I have methods like the following

[ServiceContract]
public interface MultiFormatContract
{
  [OperationContract]
  [WebGet(UriTemplate="/json/data", ResponseFormat = WebMessageFormat.Json)]
  [JSONPBehavior(callback = "callback")]
  public MySerializableObject GetJSONData()


  [OperationContract]
  [WebGet(UriTemplate = "/xml/data", ResponseFormat=WebMessageFormat.Xml)]
  public MySerializableObject GetXMLData()
}

I have a requirement to support JSONP and have used the Microsoft REST sample to support this (following this http://jasonkelly.net/archive/2009/02/24/using-jquery-amp-jsonp-for-cross-domain-ajax-with-wcf-services.aspx). However this has meant that I had to add a custom binding:

  <customBinding>
    <binding name="jsonpBinding">
      <jsonpMessageEncoding/>
      <httpTransport manualAddressing="true"/>
    </binding>
  </customBinding>

This means WCF is no longer using the textMessageEncoding and even though JSONPEncoder overrides the text encoder. My endpoints can no longer produce Xml.

I don't want to lock consumers to JSON and I'd like to be able to browse to my Xml endpoint to see the Xml for debugging etc.

Any ideas on how I can have both?

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

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

发布评论

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

评论(1

醉殇 2024-09-06 11:51:06

无需通过使用 UriTemplate 创建“虚拟”端点(例如“/xml/data”)来隔离响应类型,只需使用适当的绑定定义多个端点即可。您可以使用单个方法并将所有属性放在一个方法上。

例如

<endpoint address="json"....
<endpoint address="jsonp"....
<endpoint address="xml" ....

Instead of segregating your response types by creating a 'virtual' endpoint using UriTemplate, e.g. "/xml/data", simply define multiple endpoints with appropriate bindings. You can use a single method and put all of the attributes on one method.

e.g.

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