6

I have a an existing service like the below method:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class SomeService : ISomething
{
    public SomeListResults SomeList(SomeParams someParams)
    {
          ....
    }
}

Is there a simple way to allow JSONP calls and also JSON at the same time (detect it). Is this native?

TruMan1
  • 33,665
  • 59
  • 184
  • 335

2 Answers2

9

Update your config to look like:

<configuration>
  <system.web>
    <compilation debug="true" targetframework="4.0">
    <authentication mode="None">
  </authentication></compilation></system.web>
  <system.webserver>
    <modules runallmanagedmodulesforallrequests="true">
  </modules></system.webserver>
  <system.servicemodel>
    <servicehostingenvironment **aspnetcompatibilityenabled**="true">
    <standardendpoints>
      <webscriptendpoint>
        <standardendpoint **crossdomainscriptaccessenabled**="true" name="">
      </standardendpoint></webscriptendpoint>
    </standardendpoints>
  </servicehostingenvironment></system.servicemodel>
</configuration>

See here for a blog post providing a walkthrough of creating a wcf service that's accessible cross-domain.

This will enable your service to accept requests from cross-domain sources.

In terms of determining whether to pad your response (the p in jsonp),

Thanks to @carlosfigueira for this:

If using .Net 4 JSONP is supported natively. As long as the request has a query string parameter called "callback" (this name can be configured), the response will be padded with the function name .

Otherwise, you'll need to write a custom message inspector that pads the response appropriately.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • 1
    You don't need to write a custom inspector - in .NET 4.0 JSONP is supported natively. As long as the request has a query string parameter called "callback" (this name can be configured), the response will be padded with the function name (given that crossDomainScriptAccessEnabled is set to true, as you mentioned) – carlosfigueira Nov 22 '11 at 01:18
  • @carlosfigueira Many thanks for this nugget of information, my last forays into the world of jsonp and WCF were in .Net 3.5. Answer now updated. Great blog by the way! – Rich O'Kelly Nov 22 '11 at 10:06
2

The new JSONP feature is exposed via the WebHttpBinding. The configuration for the CustomersService would looks like this:

 <bindings>
    <webHttpBinding>
      <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
    </webHttpBinding>
  </bindings>
  <services>
    <service name="ServiceSite.CustomersService">
      <endpoint address="" binding="webHttpBinding"
                bindingConfiguration="webHttpBindingWithJsonP" contract="ServiceSite.CustomersService"
                behaviorConfiguration="webHttpBehavior"/>
    </service>
  </services>

Consuming JSONP with jQuery

 // Get the JsonP data
 $.getJSON('http://localhost:65025/CustomersService.svc/GetCustomers?callback=?', null, function (customers) {
      alert('Received ' + customers.length + ' Customers');
 });
Rachit Patel
  • 854
  • 5
  • 12