2

I'm teaching myself Blazor and have run into this conundrum, where I get this error:

{"No connection could be made because the target machine actively refused it."}

The uri I call is this one:

api/employee

However, when use the full uri, such as this:

https://localhost:44376/api/employee

I get no errors at all.

Even though this is just a practice project, I'd still prefer to use a relative uri without a port number, but am not sure how to make it work.

Here's the method where I am making these calls:

    public async Task<IEnumerable<Employee>> GetAllEmployees()
    {
            bool isEmployeeListEmpty = true; ;
            IEnumerable<Employee> employeeList = null; 

            try
            {
                //string uriEmployeeList = $"https://localhost:44376/api/employee";
                string uriEmployeeList = $"api/employee";
                var employees =  await JsonSerializer.DeserializeAsync<IEnumerable<Employee>>
                        (await _httpClient.GetStreamAsync(uriEmployeeList), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
                if (employees != null)
                {
                    isEmployeeListEmpty = false;
                    employeeList = await JsonSerializer.DeserializeAsync<IEnumerable<Employee>>
                        (await _httpClient.GetStreamAsync(uriEmployeeList), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
                }
                else 
                {
                    isEmployeeListEmpty = true;
                }

                
            }
            catch (Exception ex)
            {
                Console.WriteLine("An exception occurred inside the GetAllEmployees() method of the EmployeeDataService class");
                Console.WriteLine(ex.Message);
            }

            if (!isEmployeeListEmpty)
                return employeeList;
            else
                return null;

    }

I am doing this all on one machine with IIS Express.

UPDATE

Thank you all for your help and suggestions. It turned out that I had the ssl port defined as 44376 in the lauchSettings.json, while I had the base addresses in Startup.cs file set as https://localhost:44340/ for all three HttpClient objects I use. I changed the port in each of the base addresses to 44376 to match the 44376 port I have set up in the launchSettings.json file, and everything now works with the relative/abbreviated uri strings.

Ivan
  • 103
  • 2
  • 8
  • 2
    What is the baseaddress of the HttpClient object? – Anuraj Jul 28 '22 at 06:27
  • 1
    You have to set **Base Address** of URI, you can set this on appsettings for development (localhost:44376/) and production (domain.com/) environment, and read the setting and set base address. – mRizvandi Jul 28 '22 at 09:29
  • 1
    This post may help you: https://stackoverflow.com/a/23438417/10787774 – Jason D Jul 28 '22 at 17:47

1 Answers1

2

Please see if api and web project are set under "Multiple startup" and both are ACTION : "start" are not (Right click on Solution > Set Startup Projects), its seems like api project is not started and refuse the connection and getting similar error while accessing the api controller.

Ashutosh Soni
  • 191
  • 1
  • 4