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;