我有一个非常奇怪的情况,我一直在体验我的系统。
我在IIS中托管了asp.net web API
。 Web请求从移动设备进入,然后它将HTTP请求发送到执行特定作业的另一台服务器,然后返回结果。 (见下面的流程)
电话=> API => HTTP => (Service Executor)=> HTTP => API =>电话
这在95%的时间里完美无缺。然而,它有时会进入这种状态,其中http请求的NONE完成。
我观察到以下情况:
从我的Asp.net控制器调用以下方法:
这是我的代码:
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;
}
}
以上示例旨在每次创建和处置HttpClient
实例。我相信HttpClient
是线程安全的,我之前使用HttpClient
的单个实例进行了测试,我遇到了同样的问题。
我观察到的进一步行为。
我添加了Receives
http发布的服务器上的日志记录。当这种现象发生时,它确实收到HTTP Post,但它立即失败。几乎就像它建立连接,然后失败。
我有一些Application Insights收集的例外情况:
- 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
我真的很喜欢这个问题的一些意见,也许还有一些建议可以进一步调试/定位这个问题。