Home > Software engineering >  WebRequest to internal API in same Visual Studio solution?
WebRequest to internal API in same Visual Studio solution?

Time:10-09

I created an API .NET CORE project and it runs fine as my startup project, and has assigned 5001 as the port e.g. one of the calls is:

https://localhost:5001/api/Values/TESTME

I added a site cors policy in startup like so:

  services.AddCors(options =>
        {
            options.AddPolicy("SiteCors", builder =>
            {
                builder.AllowAnyHeader();
                builder.AllowAnyMethod();
                builder.AllowAnyOrigin();
            });
        });

Now I added another project (which runs on a different local host port) and set it as the startup project - it tries to do a WebRequest:

    string url = "https://localhost:5001/api/Values/TESTME";
    try
    {
        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        response.Close();
    }
    catch (Exception ex)
    {
        
    }

which is rejected because No connection could be made because the target machine actively refused it.

I think adding a Firewall rule may be in order but 1) how exactly do I add a firewall rule for this? and 2) would this even work? Would I need both projects to be "running" in VS at the same time?

CodePudding user response:

Make sure you run both in separate instances on separate ports & it should be fine (if you haven't explicitly blocked anything in your firewall).

You could also run one API in IIS Server or self-hosted & the other running in Visual Studio.

  • Related