title: CORS enabled WCF REST and Kendo
body: <p>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.</p>
  
  <p>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</p>
  
  <pre><code>    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();
          }
      }
  </code></pre>
  
  <p>But i need to configure the cors enabled property so i followed the this <a href="http://enable-cors.org/server_wcf.html" rel="nofollow">link</a> . 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.</p>
  
  <p>My new Service classes are;</p>
  
  <pre><code>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&lt;string, string&gt;();
  
          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&lt;string, string&gt; requiredHeaders;
      public CorsEnabledMessageInspector(Dictionary&lt;string, string&gt; headers)
      {
          requiredHeaders = headers ?? new Dictionary&lt;string, string&gt;();
      }
  
      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);
          }
      }
  }
  </code></pre>
  
  <p>My Web Configuration is </p>
  
  <pre><code>&lt;extensions&gt;
    &lt;behaviorExtensions&gt;
      &lt;add name="corsEnabledBehaviour" type="LAMI.Service.Utilities.CorsEnabledBehavior, LAMI.Service.Utilities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /&gt;
    &lt;/behaviorExtensions&gt;
  &lt;/extensions&gt;
  
  &lt;behaviors&gt;
    &lt;endpointBehaviors&gt;
      &lt;behavior name="endBehaviour1"&gt;
        &lt;webHttp helpEnabled="true" /&gt;
        &lt;corsEnabledBehaviour /&gt;
      &lt;/behavior&gt;
    &lt;/endpointBehaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name="serviceBehaviour1"&gt;
        &lt;serviceMetadata httpGetEnabled="true" /&gt;
        &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;bindings&gt;
    &lt;webHttpBinding&gt;
      &lt;binding name="webHttpConfiguration"  &gt;
      &lt;/binding&gt;
    &lt;/webHttpBinding&gt;
  &lt;/bindings&gt;
  
  &lt;services&gt;
    &lt;service behaviorConfiguration="serviceBehaviour1" name="LAMI.Service.Service1"&gt;
      &lt;endpoint address="" behaviorConfiguration="endBehaviour1" binding="webHttpBinding"
        bindingConfiguration="webHttpConfiguration" contract="LAMI.Service.Contract.IService1" /&gt;
      &lt;host&gt;
        &lt;baseAddresses&gt;
          &lt;add baseAddress="http://ltms0/ServiceApp/Service1/" /&gt;
        &lt;/baseAddresses&gt;
      &lt;/host&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  </code></pre>
  
  <p>Can you help me where i miss?</p>