0

I am trying to create Windows Service which do HTTP requests on a timer. After deploying application in production I have noticed that certain endpoint cannot receive data. After manual inspection I certainly know that I can ping, do POST request to that endpoint.

I have noticed that if I run exactly the same code, on same .NET 4.7.2 version, Console App will succeed whilst Windows Service - will not. It throws this exception:

Console APP:

using (HttpClient httpClient = new HttpClient())
 {
     httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", "xxxxxxxxxxxxxxx");
     var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
     Console.WriteLine(await content.ReadAsStringAsync());
     var response = await httpClient.PostAsync("http://xx.xx.xx.xx:8080/zzz/xx.svc/InsertData", content);
     Console.WriteLine(response.StatusCode);
}

Windows Service:

using (HttpClient httpClient = new HttpClient())
{
    httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(endpoint.AuthScheme, endpoint.AuthKey);
    var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json");
                        try
                        {
                            var response = await httpClient.PostAsync(endpoint.UrlAddress, content);
                            response.EnsureSuccessStatusCode();
                            logger.Info($"Duomenys perduoti gavėjui:{endpoint.Name}");
                        }
                        catch (Exception ex)
                        {
                            logger.Warn($"Nepavyko perduoti duomenų šaltiniui: {endpoint.Name}; Adresas:{endpoint.UrlAddress}; Duomenų kiekis:{endpoint.Tags.Count}", ex);
                        }   
}

Exception:

System.Net.Http.HttpRequestException: An error occurred while sending the request. 
---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. 
---> System.IO.IOException: Unable to read data from the transport connection: 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. 
---> 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
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
   at System.Net.PooledStream.EndRead(IAsyncResult asyncResult)
   at System.Net.Connection.ReadCallback(IAsyncResult asyncResult)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at HistorianPusher.Service.<Timer_Elapsed>d__5.MoveNext() in C:\Users\alaburda_e\source\repos\HistorianPusher\HistorianPusher\Service.cs:line 120

I can assure I check the address, auth key, scheme - everything matches 100%. I have disabled proxy, firewall.

Service installer configuration:

            // 
            // serviceProcessInstaller1
            // 
            this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.serviceProcessInstaller1.Password = null;
            this.serviceProcessInstaller1.Username = null;
            // 
            // serviceInstaller1
            // 
            this.serviceInstaller1.ServiceName = "Historian Pusher";
            this.serviceInstaller1.DisplayName = "Historian Pusher";
            this.serviceInstaller1.Description = "Procesas skirtas perduoti Historian duomenis į išorinius šaltinius naudojantis HTTP/HTTPS protokolu";
            this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
Edward
  • 93
  • 3
  • 1
    As a side note, I recommend reading [You're using HttpClient wrong and it is destabilizing your software](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) and [You're (probably still) using HttpClient wrong and it is destabilizing your software](https://josef.codes/you-are-probably-still-using-httpclient-wrong-and-it-is-destabilizing-your-software/). – ProgrammingLlama Jan 05 '21 at 07:29
  • @John Thank you, I have seen that first link you kindly provided. It's quite simple yet useful solution. It is next thing to do on my list, but firstly I got this huge problem to figure out. :( – Edward Jan 05 '21 at 08:02
  • Run Fiddler and check for any differences in the network requests. – Stephen Cleary Jan 05 '21 at 14:28

0 Answers0