问题描述
我正在尝试创建在计时器上执行 HTTP 请求的 Windows 服务。在生产中部署应用程序后,我注意到某些端点无法接收数据。手动检查后,我当然知道我可以 ping,向该端点发出 POST 请求。
我注意到,如果我在相同的 .NET 4.7.2 版本上运行完全相同的代码,控制台应用程序会成功,而 Windows 服务 - 不会。 它抛出这个异常:
控制台应用程序:
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 服务:
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),"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);
}
}
异常:
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
我可以保证我检查了地址、身份验证密钥、方案 - 一切都 100% 匹配。我已禁用代理、防火墙。
服务安装程序配置:
//
// 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;
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)