Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HTTP客户端在asp.net web api中失败_C#_Asp.net_Dotnet Httpclient - Fatal编程技术网

C# HTTP客户端在asp.net web api中失败

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应用程序池回收时间设

我在我的一个系统上一直遇到一个非常奇怪的情况

我在IIS中托管了一个
asp.net web API
。Web请求来自移动设备,然后它将HTTP请求发布到另一台执行特定作业的服务器,然后返回结果。(见下面的流程)

电话=>API=>HTTP=>(服务执行器)=>HTTP=>API=>Phone

这在95%的情况下都能正常工作。但是,它有时会进入这种状态,其中没有一个http请求完成

我观察到以下情况:

  • 在问题发生一次之后,它将继续无限期地失败,直到我重新启动应用程序池
  • 将IIS应用程序池回收时间设置为1小时,可显著减少此事件的发生。然而,它并没有完全移除
从我的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;
    }
}

我真的很想得到一些关于这个问题的信息,也许还有一些进一步的建议来尝试调试/定位这个问题。

您包含的错误消息(
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