skip to Main Content

I have a WCF service (the project is a WCF service library), the service works, I can call this service form Insomnia but when i try to use it in my next.js app, I get this error:

Access to fetch at ‘http://miURL’ 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.

I know this is a CORS problem but I don’t know hot to enable CORS in my WCF service.

This is my code:

JavaScript:

const test = async () => {
    const peticion = await fetch('URL', {
            method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      },
    })
    const respuesta = await peticion.json()
    console.log(respuesta);
  }

My Service Code:

public class Service1 : IService1
{
    public string leerHuella()
    {
        Form1 form1 = new Form1();
        string value = form1.leerHuellaForm();
        return value;
    }
}

My iService code:

public interface IService1

    {
        [OperationContract]
        [WebGet]
        string leerHuella();
    }

My App.config:

<endpointBehaviors>
  <behavior name="jsonBehavior">
    <enableWebScript />
  </behavior>
</endpointBehaviors>

If someone can help me, that would be great

I have tried some configurations from various tutorials bot none have worked so far.
As a note, I don’t have an asax.cs file and I cant add one, the option is not available in my add item window.

2

Answers


  1. You can try this solution:

    Add two new files, Global.asax and Global.asax.cs

    Global.asax:

    <%@ Application Codebehind="Global.asax.cs" Inherits="Wcf.Global" Language="C#" %>
    

    Global.asax.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.SessionState;
     
    namespace Wcf
    {
        public class Global : System.Web.HttpApplication
        {
     
            protected void Application_Start(object sender, EventArgs e)
            {
     
            }
     
            protected void Session_Start(object sender, EventArgs e)
            {
     
            }
     
            protected void Application_BeginRequest(object sender, EventArgs e)
            {
                //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost");
                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();
                }
            }
     
            protected void Application_AuthenticateRequest(object sender, EventArgs e)
            {
     
            }
     
            protected void Application_Error(object sender, EventArgs e)
            {
     
            }
     
            protected void Session_End(object sender, EventArgs e)
            {
     
            }
     
            protected void Application_End(object sender, EventArgs e)
            {
     
            }
        }
    }
    

    You can refer to this document:
    Enabling CORS in WCF

    Login or Signup to reply.
  2. This issue can also occur if your WCF is deployed on IIS and is not configured.

    1. Select the corresponding project in IIS and select HTTP Response Headers.

    enter image description here

    2.Add the appropriate information

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search