0
votes

Problem: HttpClient returns 503 "Service Unavailable" when accessing locally hosted API through console app while it can be accessed successfully using Edge. Am I missing some configurations?

Code:

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace SampleClient
{
    public class Program
    {
        private static HttpClient Client = new HttpClient();
        public static async Task Main(string[] args) 
        {
            Console.WriteLine("Starting connections");
            for(int i = 0; i<5; i++)
            {

                var result = await Client.GetAsync("http://localhost:5100/api/contacts");
                //var result = await Client.GetAsync("https://stackoverflow.com/");
                Console.WriteLine(result.StatusCode);
            }
            Console.WriteLine("Connections done");
        }
    }
}

--- build ---

dotnet build

Microsoft (R) Build Engine version 16.5.0-preview-20064-06+86d9494e4 for .NET Core Copyright (C) Microsoft Corporation. All rights reserved.

...

Build succeeded. 0 Warning(s) 0 Error(s)

sample output: screenshot

clear headers

1
in your api side, please enable debug level log and check if there is any error in logElendil Zheng-MSFT
What about if you try add Client.DefaultRequestHeaders.Accept.Clear(); Client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); before you calling GetAsync?Selim Yildiz
Even if I cleared the header, still it is error.McKoy
@ElendilZheng-MSFT I think API is working properly. As shown in the screenshots, it can be displayed in Edge. (even in firefox, chrome and postman)McKoy
Did you start your .net core hosted instance or IIS website/apppool prior to executing the Get to localhost:5100?Stinky Towel

1 Answers

0
votes

Sorry. The cause is the Antivirus firewall. After disabling, I was able to access successfully.