问题
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