使用工厂和 JSONP 的无配置 WCF

发布于 2024-08-22 21:40:31 字数 526 浏览 2 评论 0原文

我在 WCF 服务中使用 WebServiceHostFactory 以避免在 web.config 中创建绑定配置的垃圾。

但是,我想将服务公开为 XML/JSON 和 JSONP。

阅读: http://jasonkelly.net/archive/2009/02/24/using-jquery-amp-jsonp-for-cross-domain-ajax-with-wcf-services.aspx

它不看起来我可以扩展 WCF 来添加 JSONP,而无需求助于大量的自定义绑定配置。

那么,对于那些已经完成此操作的人来说,是否有可能拥有一个根据 UriTemplate 以 XML/JSON/JSONP 进行响应的宁静 WCF 服务,而无需诉诸大量的配置连接?

I'm using the WebServiceHostFactory in my WCF services to avoid having to create a crapton of binding configuration in web.config.

However, I'd like to expose the services as XML/JSON and JSONP.

Reading: http://jasonkelly.net/archive/2009/02/24/using-jquery-amp-jsonp-for-cross-domain-ajax-with-wcf-services.aspx

It does not look like I can extend WCF to add JSONP without resorting to a mountain of custom binding config.

So, for those who have done it, is it possible to have a restful WCF service that responds in XML/JSON/JSONP depending on the UriTemplate, without resorting to a ton of config wiring?

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

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

发布评论

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

评论(3

待"谢繁草 2024-08-29 21:40:31
  1. JSONP 仅可通过 NET4 之前的自定义绑定使用。在 .NET 4 中,他们在 WebHttpBinding 上添加了一个名为 crossDomainScriptAccessEnabled 的新属性,该属性增加了对 JSONP 的支持。请参阅 http://www.bendewey .com/blog/index.php/186/using-jsonp-with-wcf-and-jquery

  2. 至于使用 UriTemplates 在一项服务中接受 XML 和 JSON,我在此演示中描述了两种技术 http://www.bendewey.com/blog/index.php /176/alt-net-rest-presentation(完整的源代码也可以在这里找到)。

    1. 使用两个入口方法并使用内部方法处理调用。请参阅示例 1。

    2. 使用包罗万象的消息输入/输出合同并手动路由服务调用。请参阅示例 2。

样品 1

    [OperationContract]
    [WebGet(UriTemplate = "/")]
    Years GetYears();

    [OperationContract]
    [WebGet(UriTemplate = "/json/", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    Years GetJsonYears();

    Years GetYearsInternal();

样品 2

    [OperationContract]
    [WebGet(UriTemplate = "*")]
    Message Get();
  1. JSONP is only available through custom binding pre-NET4. With .NET 4 they've added a new property on the WebHttpBinding called crossDomainScriptAccessEnabled that adds support for JSONP. See http://www.bendewey.com/blog/index.php/186/using-jsonp-with-wcf-and-jquery

  2. As for accepting XML and JSON in one service using UriTemplates I describe two techniques in this presentaion http://www.bendewey.com/blog/index.php/176/alt-net-rest-presentation (Full Source code also available here).

    1. Use two entry methods and handle the call with an internal method. See Sample 1.

    2. Use a catch-all Message in/out contract and route the service call manually. See sample 2.

Sample 1

    [OperationContract]
    [WebGet(UriTemplate = "/")]
    Years GetYears();

    [OperationContract]
    [WebGet(UriTemplate = "/json/", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    Years GetJsonYears();

    Years GetYearsInternal();

Sample 2

    [OperationContract]
    [WebGet(UriTemplate = "*")]
    Message Get();
花之痕靓丽 2024-08-29 21:40:31

我认为您应该能够通过在服务合同中使用具有不同响应格式的不同方法来轻松地做到这一点:

interface IYourService
{
     [OperationContract]
     [WebGet(UriTemplate="/YourMethod/XML", ResponseFormat=WebMessageFormat.Xml)]
     SomeReturnObject YourMethodAsXml(.....);

     [OperationContract]
     [WebGet(UriTemplate="/YourMethod/JSON", ResponseFormat=WebMessageFormat.Json)]
     SomeReturnObject YourMethodAsJson(.....);
}

然后这两种方法都可以调用一个通用的核心函数,该函数执行数据的实际计算/查找或您正在做的任何事情。

我想说……这不会涉及大的配置战争,它会解决您三点中的至少两点(XML 和 JSON)。

WCF 本身不支持 JSONP - 但正如您引用的文章所示,您可以相当轻松地添加此行为。不过,这确实需要一些配置连接才能启用此 WCF 扩展。但据我所知,这应该是您服务器上的一次性事件。

如果您确实无法处理此配置设置,您当然可以从用于 WCF REST 服务的 WebServiceHostFactory 派生自定义 WebServiceHostFactoryWithJSONPSupport,并添加必要的扩展(例如服务行为等)到您的主机工厂。该类不是密封的,因此应该足够简单(至少在理论上:-))。

I think you should be able to do that pretty easily by having different methods in your service contract which have differing response formats:

interface IYourService
{
     [OperationContract]
     [WebGet(UriTemplate="/YourMethod/XML", ResponseFormat=WebMessageFormat.Xml)]
     SomeReturnObject YourMethodAsXml(.....);

     [OperationContract]
     [WebGet(UriTemplate="/YourMethod/JSON", ResponseFormat=WebMessageFormat.Json)]
     SomeReturnObject YourMethodAsJson(.....);
}

and both methods could then call a common core function which does the actual computation / lookup of data or whatever you're doing.

No big config war invovled for this, I'd say.... and it would solve at least two of your three points (XML and JSON).

JSONP isn't natively supported in WCF - but as the article you referenced shows, you can fairly easily add this behavior. This does require some config wiring up, to enable this WCF extension, though. But it should be a one-time thing on your server, as far as I can see.

If you really can't deal with this config setup, you could of course derive a custom WebServiceHostFactoryWithJSONPSupport from the WebServiceHostFactory used for the WCF REST services, and add the necessary extensions (like service behaviors etc.) to your host factory. The class is not sealed, so that should be simple enough (at least in theory :-) ).

千年*琉璃梦 2024-08-29 21:40:31

只是为了让您知道 WCF 数据服务(前身为 Astoria)直接支持 OData 和 JSON。

不需要任何编码。在 VS 2010 中,您只需:

  1. 创建 ADO.NET 实体模型
  2. 创建新的 WCF 数据服务

这会自动创建一个基于 REST 的 Web 服务,该服务根据提供的配置发出 JSON 或 Atom。默认为 ATOM,要获取 JSON 格式的数据,您必须在客户端“Accept”标头中指定“application/json”。

要制作 JSONP,需要做一些工作,请检查此链接:
链接 1

just to let you know that WCF Data services (former Astoria) supports directliy OData and JSON.

no need for any coding. In VS 2010 you just:

  1. Create ADO.NET Entity model
  2. Create new WCF Data Service

This automatically creates a REST-ful Web service that emits JSON or Atom based on a provided configuration. The default is ATOM, to get JSON-formatted data, you have to specify "application/json" in your client "Accept" header.

To make JSONP, some work is needed, check this link:
Link 1

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