C# HTTP客户端在asp.net web api中失败
我在我的一个系统上一直遇到一个非常奇怪的情况 我在IIS中托管了一个C# HTTP客户端在asp.net web api中失败,c#,asp.net,dotnet-httpclient,C#,Asp.net,Dotnet Httpclient,我在我的一个系统上一直遇到一个非常奇怪的情况 我在IIS中托管了一个asp.net web API。Web请求来自移动设备,然后它将HTTP请求发布到另一台执行特定作业的服务器,然后返回结果。(见下面的流程) 电话=>API=>HTTP=>(服务执行器)=>HTTP=>API=>Phone 这在95%的情况下都能正常工作。但是,它有时会进入这种状态,其中没有一个http请求完成 我观察到以下情况: 在问题发生一次之后,它将继续无限期地失败,直到我重新启动应用程序池 将IIS应用程序池回收时间设
asp.net web API
。Web请求来自移动设备,然后它将HTTP请求发布到另一台执行特定作业的服务器,然后返回结果。(见下面的流程)
电话=>API=>HTTP=>(服务执行器)=>HTTP=>API=>Phone
这在95%的情况下都能正常工作。但是,它有时会进入这种状态,其中没有一个http请求完成
我观察到以下情况:
- 在问题发生一次之后,它将继续无限期地失败,直到我重新启动应用程序池
- 将IIS应用程序池回收时间设置为1小时,可显著减少此事件的发生。然而,它并没有完全移除
public static async Task<string> MakeRequest(string deviceId, BaseCommand baseCommand)
{
using (var client = new HttpClient())
{
// Setup Client
client.BaseAddress = await GetBaseAddressForDevice(deviceId);
client.DefaultRequestHeaders.Accept.Clear();
//Only wait 90 seconds for responses
client.Timeout = new TimeSpan(0, 0, 0, 90);
var requestId = Guid.NewGuid();
Debug.WriteLine(">Posting RequestId: " + requestId + " commandId:" + ((int)(baseCommand.CommandId)).ToString(CultureInfo.InvariantCulture) + " data: " + JsonConvert.SerializeObject(baseCommand));
//
// Make request
HttpResponseMessage response = null;
string responseContent = null;
try
{
response = await client.PostAsJsonAsync(((int)baseCommand.CommandId).ToString(CultureInfo.InvariantCulture), baseCommand);
// read response content
responseContent = await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
//TODO: Log excepton
throw ex;
}
Debug.WriteLine(">Response RequestId: " + requestId + " data: " + JsonConvert.SerializeObject(baseCommand));
//
if (response.IsSuccessStatusCode)
{
return responseContent;
}
HandleErrorResponse(response, responseContent);
//Wont hit here as an exception will be thrown
return null;
}
}
我真的很想得到一些关于这个问题的信息,也许还有一些进一步的建议来尝试调试/定位这个问题。您包含的错误消息(
System.Net.Sockets.SocketException
),是由Web Api还是它调用的服务器记录的?服务器代码中的哪一行抛出异常?您是否看到、阅读并测试了作者所说的内容。似乎值得一试。您包含的错误消息(System.Net.Sockets.SocketException
)是由Web Api或它调用的服务器记录的吗?服务器代码中的哪一行抛出异常?您是否看到、阅读并测试了作者所说的内容。看来值得一试。
- System.Net.Sockets.SocketException
An error occurred while sending the request.The underlying connection was closed: An unexpected error occurred on a receive.Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.An existing connection was forcibly closed by the remote host