Jquery CORS支持WCF休息和剑道

Jquery CORS支持WCF休息和剑道,jquery,cors,wcf-rest,kendo-dataviz,Jquery,Cors,Wcf Rest,Kendo Dataviz,我在wcf休息服务公司工作。我需要允许cors支持跨域访问。我读了文章,得到了2种方法 第一种方法是在global.asax中的应用程序_beginrequest事件中添加http头。这对我来说很好。我使用jquery使用剑道图绑定测试了这一点。图表填充在IE、Chrome和Firefox中。在global.asax中启用cors的工作代码为 protected void Application_BeginRequest(object sender, EventArgs e) {

我在wcf休息服务公司工作。我需要允许cors支持跨域访问。我读了文章,得到了2种方法

第一种方法是在global.asax中的应用程序_beginrequest事件中添加http头。这对我来说很好。我使用jquery使用剑道图绑定测试了这一点。图表填充在IE、Chrome和Firefox中。在global.asax中启用cors的工作代码为

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            //These headers are handling the "pre-flight" OPTIONS call sent by the browser
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Authorization, Origin, Content-Type, Accept, X-Requested-With");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }
但是我需要配置cors enabled属性,所以我遵循了这个步骤。我复制并运行了成功的服务。但在我在endpoint中启用此行为后,客户端在Chrome和Firefox中没有显示图表。因此,未启用跨域。我说得对吗?我想念这里

我的新服务课程是

public class CorsEnabledBehavior : BehaviorExtensionElement, IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {

    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {

    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
        var requiredHeaders = new Dictionary<string, string>();

        requiredHeaders.Add("Access-Control-Allow-Origin", "*");
        requiredHeaders.Add("Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS");
        requiredHeaders.Add("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");

        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new CorsEnabledMessageInspector(requiredHeaders));
    }

    public void Validate(ServiceEndpoint endpoint)
    {

    }

    public override Type BehaviorType
    {
        get { return typeof(CorsEnabledBehavior); }
    }

    protected override object CreateBehavior()
    {
        return new CorsEnabledBehavior();
    }

}

public class CorsEnabledMessageInspector : IDispatchMessageInspector
{
    Dictionary<string, string> requiredHeaders;
    public CorsEnabledMessageInspector(Dictionary<string, string> headers)
    {
        requiredHeaders = headers ?? new Dictionary<string, string>();
    }

    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
    {
        return null;
    }

    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
        var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
        foreach (var item in requiredHeaders)
        {
            httpHeader.Headers.Add(item.Key, item.Value);
        }
    }
}
公共类CorsEnabledBehavior:BehaviorExtensionElement,IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint端点,System.ServiceModel.Channel.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint端点,System.ServiceModel.Dispatcher.ClientRuntime ClientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint端点,System.ServiceModel.Dispatcher.EndpointDispatcher EndpointDispatcher)
{
var requiredHeaders=新字典();
requiredHeaders.Add(“访问控制允许来源”、“*”);
添加(“访问控制请求方法”、“POST、GET、PUT、DELETE、OPTIONS”);
requiredHeaders.Add(“访问控制允许头”,“X请求的,内容类型”);
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(新的CorsEnabledMessageInspector(requiredHeaders));
}
公共void验证(ServiceEndpoint)
{
}
公共重写类型BehaviorType
{
获取{return typeof(CorsEnabledBehavior);}
}
受保护的重写对象CreateBehavior()
{
返回新的CorsEnabledBehavior();
}
}
公共类CorsEnabledMessageInspector:IDispatchMessageInspector
{
词典要求的标题;
public CorsEnabledMessageInspector(字典标题)
{
requiredHeaders=标题??新字典();
}
公共对象AfterReceiveRequest(参考System.ServiceModel.Channels.Message request、System.ServiceModel.IClientChannel、System.ServiceModel.InstanceContext InstanceContext)
{
返回null;
}
SendReply之前的公共无效(参考System.ServiceModel.Channels.Message reply,object correlationState)
{
var httpHeader=reply.Properties[“httpResponse”]作为HttpResponseMessageProperty;
foreach(RequiredHeader中的var项)
{
httpHeader.Headers.Add(item.Key,item.Value);
}
}
}
我的网络配置是

<extensions>
  <behaviorExtensions>
    <add name="corsEnabledBehaviour" type="LAMI.Service.Utilities.CorsEnabledBehavior, LAMI.Service.Utilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </behaviorExtensions>
</extensions>

<behaviors>
  <endpointBehaviors>
    <behavior name="endBehaviour1">
      <webHttp helpEnabled="true" />
      <corsEnabledBehaviour />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="serviceBehaviour1">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<bindings>
  <webHttpBinding>
    <binding name="webHttpConfiguration"  >
    </binding>
  </webHttpBinding>
</bindings>

<services>
  <service behaviorConfiguration="serviceBehaviour1" name="LAMI.Service.Service1">
    <endpoint address="" behaviorConfiguration="endBehaviour1" binding="webHttpBinding"
      bindingConfiguration="webHttpConfiguration" contract="LAMI.Service.Contract.IService1" />
    <host>
      <baseAddresses>
        <add baseAddress="http://ltms0/ServiceApp/Service1/" />
      </baseAddresses>
    </host>
  </service>
</services>


你能帮我找到我想念的地方吗?

我做完了。我使用global.asax启用了cors并成功运行。问题是iecors.js。在IE 8和9中启用cors不起作用。所有其他浏览器工作正常

我想给你投票10000次。我尝试了所有方法-配置文件、自定义IDispatchMessageInspector来添加标题(与您在asax中添加的标题/值相同)-“GET”始终有效,但其他所有方法都被取消。将此移动到。asax解决了此问题,谢谢!请在这方面帮助我:-