Configurationless WCF using Factories and JSONP
I'm using the WebServiceHostFactory in my WCF services to avoid having to creat开发者_如何学Ce 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?
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
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).
Use two entry methods and handle the call with an internal method. See Sample 1.
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();
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 :-) ).
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:
- Create ADO.NET Entity model
- 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
精彩评论