WCF 服务中的 JSONPBehaviour 和多种格式
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无需通过使用 UriTemplate 创建“虚拟”端点(例如“/xml/data”)来隔离响应类型,只需使用适当的绑定定义多个端点即可。您可以使用单个方法并将所有属性放在一个方法上。
例如
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.