title: Enabling OPTIONS method in CORS during REST request from AJAX on WCF Service
body: <p>I have scratched my head for 7 hours trying to figure this out. I have searched all over the web but no luck. I have an Angular App that is making requests to a WCF command-line hosted service application. I managed to get by CORS by using these two classes:</p>
  
  <pre><code>public class CustomHeaderMessageInspector : IDispatchMessageInspector
  {
      Dictionary&lt;string, string&gt; requiredHeaders;
  
      public CustomHeaderMessageInspector(Dictionary&lt;string, string&gt; headers)
      {
          requiredHeaders = headers ?? new Dictionary&lt;string, string&gt;();
      }
  
      public object AfterReceiveRequest(ref Message request, 
      System.ServiceModel.IClientChannel channel, 
      System.ServiceModel.InstanceContext instanceContext)
      {
          return null;
      }
  
      public void BeforeSendReply(ref 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>And:</p>
  
  <pre><code>public class EnableCorsBehavior : 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 CustomHeaderMessageInspector(requiredHeaders));
      }
  
      public void Validate(ServiceEndpoint endpoint) { }
  
      public override Type BehaviorType
      {
          get { return typeof(EnableCorsBehavior); }
      }
  
      protected override object CreateBehavior()
      {
          return new EnableCorsBehavior();
      }
  }
  </code></pre>
  
  <p>Adding this custom extension to the app.config file solved my CORS problem. My current problem is whenever I make a POST request, I get the error:</p>
  
  <pre><code>Request Method:OPTIONS
  Status Code:405 Method Not Allowed
  </code></pre>
  
  <p>I am quite new to C# and I can't seem to find where to place the code that will allow me to get past this. I have an idea that it should be placed somewhere in the BeforeSendReply() method. Please help me! I will really really appreciate it!</p>
  
  <p>Regards!</p>