CORS enabled WCF REST and Kendo



我在WCF REST服务中工作。我需要允许CORS Supprt进行跨域访问。我阅读了文章并有2种方法。

第一个方法是在global.asax中的application_beginrequest事件中添加HTTP标头。这对我来说很好。我使用jQuery使用kendo图表进行了测试。图表以IE,Chrome和Firefox为单位。在global.asax中启用COR的工作代码是

    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的属性,因此我遵循此链接。我复制并运行了成功的服务。但是,在我启用了此行为之后,客户端没有在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);
        }
    }
}

我的Web配置是

<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。所有其他浏览器都可以正常工作

相关内容

  • 没有找到相关文章

最新更新