Search code examples
jsonpwcf-ria-services

how can I add a JSONP endpoing for WCF Ria Services to enable cross-domain calls?


I'm aware that WCF RIA Services has a Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory that I can use to enable JSON. I need to enable cross-domain calls via JSONP. Is there an existing DomainServiceEndpointFactory that will accomplish this?


Solution

  • I just needed to do this - I overrode JsonEndpointFactory and tinkered with the binding in there, then added an endpoint using the new class.

    namespace Bodge
    {
        public class JsonPEndpointFactory : JsonEndpointFactory
        {
            public override IEnumerable<ServiceEndpoint> CreateEndpoints(DomainServiceDescription description, DomainServiceHost serviceHost)
            {
                IEnumerable<ServiceEndpoint> endPoints = base.CreateEndpoints(description, serviceHost);
                foreach (ServiceEndpoint endPoint in endPoints)
                {
                    if (endPoint.Binding is WebHttpBinding)
                    {
                        ((WebHttpBinding)endPoint.Binding).CrossDomainScriptAccessEnabled = true;
                    }
                }
    
                return endPoints;
            }
        }
    }
    
      <endpoints>
        <add name="JSONP" type="Bodge.JsonPEndpointFactory, Bodge, Version=1.0.0.0"/>
      </endpoints>
    

    Then access your service with the endpoint and the callback query param e.g. http://blah/service.svc/JSONP/GetStuff?callback=callbackname

    Hope that helps, Chris.