Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No connection could be made because the target machine actively refused it." error in Blazor project

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.

like image 525
Ivan Avatar asked Oct 19 '25 11:10

Ivan


1 Answers

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.

like image 145
Ashutosh Soni Avatar answered Oct 21 '25 02:10

Ashutosh Soni