Question

I am working in wcf rest service. I need to allow cors supprt for cross domain access. I read the articles and got 2 method.

First method is to add http headers in the application_beginrequest event in global.asax. This is working fine for me. I tested this using a kendo chart bind using jquery. Chart is populated in IE, Chrome and Firefox. The working code for enabling cors in global.asax is

    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();
        }
    }

But i need to configure the cors enabled property so i followed the this link . I copied and run the service which is success. But after i enabled this behaviour in endpoint the client didn't show chart in Chrome and Firefox. So cross domain is not enabled. Am i right? Where i miss here.

My new Service classes are;

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);
        }
    }
}

My Web Configuration is

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

Can you help me where i miss?

Was it helpful?

Solution

I finished this. I enabled cors using global.asax and working successfully. The problem is iecors.js. This is not working to enable cors in IE 8 and 9. All other browsers working fine

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top