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

匆匆过客 提交于 2019-12-09 00:52:30

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.

Formatting in comments isn't great, so for future reference here are the required usings and assemblies.

Thanks very much, that's exactly what I needed!for future reference, these are the using statements:

Namespaces:

using System.Web;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using Microsoft.ServiceModel.DomainServices.Hosting;

Assemblies

NETFX 4.0

System.ServiceModel
System.ServiceModel.Web

WCF RIA Services V1.0 SP2 RC

System.ServiceModel.DomainServices.Hosting
System.ServiceModel.DomainServices.Server

WCF RIA Services Toolkit (September 2011)

Microsoft.ServiceModel.DomainServices.Hosting
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!