tags:

views:

637

answers:

4

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?

+1  A: 

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 :-) ).

marc_s
I think I'll resort to the custom serviceHostFactory. That seems fairly easy enough.
FlySwat
+2  A: 
  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();
bendewey
A: 

Hi I have followed the steps to resolving cross domain issue using jsonp on rest wcf service. This is my function in wcf service

[OperationContract]

[WebInvoke(ResponseFormat = WebMessageFormat.Json)]

res[] GetBooth(res ss);

[DataContract]
public class res
{
    [DataMember]
    public string title { get; set; }

    [DataMember]
    public string desc { get; set; }

}

public res[] GetData(res val ) {
List ress = new List(); return ss.title; return ress.ToArray(); } Now I want to pass function parameter while calling from client as class objects. function GetData() {

var reqParams = {
    title: "hello"
};
var reqParams_Serialized = Sys.Serialization.JavaScriptSerializer.serialize(reqParams);
$.ajax({
    type: "Post",
    cache: false,
    url: "http://localhost:50933/test.svc/jsonp/getdata ",
    scriptCharset: "utf-8",
    contentType: 'application/json;charset=utf-8',
    dataType: "json",
    data: reqParams,
    success: function(data, textStatus) {
        alert(data);

    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert('error');
    }
});

}}

But when pass parameters like this it is not working. Please suggest me something with example or modification in this code.

Thanks Parveen Punia

parveenpunia
A: 

Hi,

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

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 JASONP, some work is needed, 2 options here:

josheinstein.com/blog/index.php/2010/05/wcf-data-services-format-json/

and here:

code.msdn.microsoft.com/DataServicesJSONP

cheers Valko

Valko