title: WCF CORS is set but still getting No 'Access-Control-Allow-Origin' header is present
body: <p>I am getting this error when attempting to consume a wcf service cross-domain:</p>
  
  <blockquote>
    <p>XMLHttpRequest cannot load <a href="https://MyDomain/MyService.svc/GetData" rel="nofollow">https://MyDomain/MyService.svc/GetData</a>. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="https://CallingDomain" rel="nofollow">https://CallingDomain</a>' is therefore not allowed access. The response had HTTP status code 500.</p>
  </blockquote>
  
  <p>I already have set up CORS access in the web.config file like so:</p>
  
  <pre><code>    &lt;httpProtocol&gt;
        &lt;customHeaders&gt;
          &lt;add name="Access-Control-Allow-Origin" value="*"/&gt;
          &lt;add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT" /&gt;
          &lt;add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/&gt;
        &lt;/customHeaders&gt;
      &lt;/httpProtocol&gt;
  </code></pre>
  
  <p>Here is my full web.config file:</p>
  
  <pre><code>     &lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;
        &lt;section name="MyService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/&gt;
      &lt;/sectionGroup&gt;
    &lt;/configSections&gt;
  
  
    &lt;appSettings&gt;
      &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /&gt;
    &lt;/appSettings&gt;
  
    &lt;system.web&gt;
      &lt;customErrors mode="Off"/&gt;
      &lt;compilation targetFramework="4.5" debug="true"/&gt;
      &lt;httpRuntime targetFramework="4.5"/&gt;
      &lt;pages controlRenderingCompatibilityVersion="4.0"/&gt;
      &lt;webServices&gt;
        &lt;protocols&gt;
          &lt;add name="HttpPost" /&gt;
          &lt;add name="HttpSoap" /&gt;
          &lt;add name="HttpGet" /&gt;
        &lt;/protocols&gt;
      &lt;/webServices&gt;
    &lt;/system.web&gt;
    &lt;!-- Diagnostics Configuration--&gt;
    &lt;system.diagnostics&gt;
      &lt;sources&gt;
        &lt;source name="System.ServiceModel.MessageLogging" switchValue="Error,ActivityTracing"&gt;
          &lt;listeners&gt;
            &lt;add type="System.Diagnostics.DefaultTraceListener" name="Default"&gt;
              &lt;filter type=""/&gt;
            &lt;/add&gt;
            &lt;add name="ServiceModelMessageLoggingListener"&gt;
              &lt;filter type=""/&gt;
            &lt;/add&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
      &lt;/sources&gt;
      &lt;sharedListeners&gt;
        &lt;add initializeData="E:\MyErrorFile.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp"/&gt;
      &lt;/sharedListeners&gt;
      &lt;trace autoflush="true"/&gt;
    &lt;/system.diagnostics&gt;
    &lt;appSettings&gt;
      &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/&gt;
      &lt;add key="MajorVersion" value="1"/&gt;
      &lt;add key="MinorVersion" value="1"/&gt;
    &lt;/appSettings&gt;
    &lt;system.serviceModel&gt;
  
  
      &lt;extensions&gt;
      &lt;behaviorExtensions&gt;
        &lt;add
          name="crossOriginResourceSharingBehavior"
          type="MyNameSpace.CORSEnablingBehavior, MyNameSpace, Version=1.0.0.0, Culture=neutral" /&gt;
      &lt;/behaviorExtensions&gt;
    &lt;/extensions&gt;
  
  
      &lt;services&gt;
        &lt;service name="MyNameSpace.MyService" behaviorConfiguration="ServiceBehaviour"&gt;
          &lt;endpoint address="" binding="webHttpBinding" contract="MyNameSpace.MyService" behaviorConfiguration="web"/&gt;
          &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt;
  
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;bindings&gt;
        &lt;webHttpBinding&gt;
          &lt;binding name="webHttpBinding"&gt;
            &lt;security mode="TransportCredentialOnly"&gt;
              &lt;transport clientCredentialType="None" /&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/webHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="ServiceBehaviour"&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="web"&gt;
            &lt;webHttp/&gt;
            &lt;crossOriginResourceSharingBehavior /&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;protocolMapping&gt;
        &lt;add binding="basicHttpsBinding" scheme="https" /&gt;
      &lt;/protocolMapping&gt;
      &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/&gt;
      &lt;standardEndpoints&gt;
        &lt;webScriptEndpoint&gt;
          &lt;standardEndpoint name="" crossDomainScriptAccessEnabled="true" /&gt;
        &lt;/webScriptEndpoint&gt;
      &lt;/standardEndpoints&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true" /&gt;
  
  
      &lt;directoryBrowse enabled="true"/&gt;
      &lt;httpProtocol&gt;
        &lt;customHeaders&gt;
          &lt;add name="Access-Control-Allow-Origin" value="*"/&gt;
          &lt;add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT" /&gt;
          &lt;add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/&gt;
        &lt;/customHeaders&gt;
      &lt;/httpProtocol&gt;
    &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Here is my MyService.svc.cs file:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Runtime.Serialization;
  using System.ServiceModel;
  using System.ServiceModel.Web;
  using System.Text;
  using System.ServiceModel.Activation;
  
  namespace MyNameSpace
  {
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]    
      [ServiceContract]
      public class MyService
      {        
          [WebInvoke(Method = "POST",
                      ResponseFormat = WebMessageFormat.Json,
                      BodyStyle = WebMessageBodyStyle.Bare,
                      UriTemplate = "/GetData"
                     )]
          [OperationContract]
          public TestData GetData(string value)
          {
              TestData tsd = TestData.GetTestData(value);
  
              return tsd;
          } 
      }
  }
  </code></pre>
  
  <p>Here is the ajax I am using to consume the service from a different domain:</p>
  
  <pre><code>function CallService() {
                  var params = new Object();
                  params.value = "1234";
  
                  var url = "https://ServiceURL/MyService.svc/GetData";
  
                  $.ajax({
                      type: "POST",
                      contentType: "application/json;charset=utf-8",                        
                      url: url,                        
                      data: JSON.stringify(params),                        
                      dataType: "json",
                      success: function (response) {
                          var results = response.d;
  
                          $.each($(results), function (key, value) {
                              alert(value.Last_Name + " " + value.First_Name);
                          });
  
                      },
                      error: function (a, b, c) {
  
                          var warnmsg = "Unable to call service: " + a.responseText;
                          alert(warnmsg);
  
                      }
                  });
  
              }
  </code></pre>
  
  <p>I have even also tried setting up CORS through a global.asax file like so:</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")
          {
              HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");
  
              HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
              HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
              HttpContext.Current.Response.End();
          }
      }
  </code></pre>
  
  <p>Can anyone clue me in to what I am missing here in order to call this service from a different domain?</p>