title: How to enable Cross-Origin Resource Sharing in .Net Console Application WCF Service?
body: <p>I have a .netframework(4.5.2) Console Application which have RESTful WCF Service.</p>
  
  <p>I have a problem with using rest service from Javascript client.</p>
  
  <p>When I use Postman to consume rest service, there is no problem.</p>
  
  <p>When I use Javascript fetch method, there is a CORS error</p>
  
  <pre><code>from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
  </code></pre>
  
  <p>I tried below solution from google;</p>
  
  <p><strong>1- Adding <code>Web.config customHeaders</code>.</strong></p>
  
  <p><a href="https://jasonstcyr.com/2014/07/07/wcf-and-cors-no-access-control-allow-origin-header-is-present-on-the-requested-resource/" rel="nofollow noreferrer">Web.config adding parameter</a></p>
  
  <p>however, there is no web.config, i add the below code App.config</p>
  
  <pre><code>&lt;httpProtocol&gt;
  &lt;customHeaders&gt;
      &lt;add name="Access-Control-Allow-Origin" value="*"/&gt;
      &lt;add name="Access-Control-Allow-Headers" value="Content-Type, Accept" /&gt;
      &lt;add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" /&gt;
      &lt;add name="Access-Control-Max-Age" value="1728000" /&gt;
  &lt;/customHeaders&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>2- Global.asax</strong> </p>
  
  <p><a href="https://www.codeproject.com/Articles/845474/Enabling-CORS-in-WCF" rel="nofollow noreferrer">Global.asax solution for a web project</a></p>
  
  <p>Because of reason mentioned before, there is no Global.asax.
  I cant try this.</p>
  
  <p><strong>3- WCF Builder</strong></p>
  
  <p>I allowed this CrossDomain control when build wcf service. This is not workin too.</p>
  
  <pre><code> var binding = new WebHttpBinding(WebHttpSecurityMode.None);
   binding.CrossDomainScriptAccessEnabled = true;
  </code></pre>
  
  <p>Thanks for the advice.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I also create a test application on github. You can see there Postman request reach service method, but javascript request does not. It gives below error.</p>
  
  <p><a href="https://github.com/mmustafau/StackoverServiceTestnet" rel="nofollow noreferrer">https://github.com/mmustafau/StackoverServiceTestnet</a></p>
  
  <p>...has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.</p>
  
  <p>my javascript request is below.</p>
  
  <pre><code> let receiptJson =   {
          "Email": "asdas@asdas.com",
          "Name": "asdasd",
          "Password": "asdasd"
      }
  
    const requestOptions = {
          method: 'POST',
          headers:{ 'Content-Type': 'application/json',
               },
          body: JSON.stringify (receiptJson)
  
      };
  
  return  fetch("http://localhost:8070/ServiceModelSamples/service/user", requestOptions)
          .then(handleResponse)
          .then(receiptJson =&gt; {
  
  
              return receiptJson;
          });
  </code></pre>