Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling WebApi from C# getting exception HttpRequestException

Below code is a simple ASP.NET website hosted on IIS. When I run the website from local machine it works fine for both environmentName (UAT and SYSTEM). But when hosted on IIS, I get error while calling WebApi for SYSTEM environment and UAT works fine.

        string uri = string.Empty;
        StringBuilder sbResult = new StringBuilder();
        string message = string.Empty;

        try
        {
            using (HttpClient client = new HttpClient())
            {
                if (environmentName.ToLower().Contains("system"))
                    uri = "http://172.26.25.53:5453/"; // SYS
                else
                    uri = "http://172.26.129.21:5453/"; // UAT

                client.BaseAddress = new Uri(uri);

                var response = client.GetAsync("api/AppFabric").Result;

                if (response.IsSuccessStatusCode)
                {

                }
            }
        }

When I run this code for environmentName "UAT", it runs fine. But when environment name is "SYSTEM" I get exception as below

System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 172.26.25.53:5453 at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)

Please help, I am new to WebApi. Is there I am missing something. I have hosted WebApi on both environments in same way.

like image 870
keyur sheth Avatar asked Mar 02 '26 11:03

keyur sheth


1 Answers

I found this was a timeout problem. To fix this I increased the connection time in IIS. In IIS I went to the default website, right clicked --> Manage Website --> Advanced Settings --> Connection Limits (expand) --> change the default from 120 to (say) 180.

like image 91
arame3333 Avatar answered Mar 04 '26 01:03

arame3333



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!