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

好久不见. 提交于 2019-12-08 05:57:42

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/8823649/how-can-i-add-a-jsonp-endpoing-for-wcf-ria-services-to-enable-cross-domain-calls

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