GetAsync导致超时问题

时间:2016-08-31 13:37:40

标签: c# asp.net-mvc asp.net-web-api async-await

我有以下代码,这会在获取数据时导致问题

 var result = client.GetAsync(requestUri).Result;

如果我从浏览器点击requestUri,那么它可以正常工作。但是从上面的代码中它给出了以下错误。

错误:

  

发生了一个或多个错误。发送请求时发生错误。基础连接已关闭:发送时发生意外错误。由于远程方已关闭传输流,验证失败。

Stack Trace1:

  

at System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at foo(String resultUrl) at dooo(String param, String resultUrl) at foo.<>c__DisplayClass2.<Product>b__0(String x, String y) at foo[T](Func 3 fn)at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)      System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)上的System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)      在System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)

Stack Trace2

  

at System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification) at Foo1(String resultUrl) at Foo2(String param, String resultUrl) at Foo3.<>c__DisplayClass2.<Product>b__0(String x, String y) at Foo4(Func 3 fn)at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)      System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)上的System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)      在System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)         在System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()      在System.Web.Http.Filters.ActionFilterAttribute.d__0.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()      在System.Web.Http.Filters.ActionFilterAttribute.d__5.MoveNext()

我也尝试过等待但没有运气。在这种情况下,我们是否可以增加get请求的请求超时。

我试过的等待代码

var result = await client.GetAsync(requestUri);
                if (!result.IsSuccessStatusCode) throw new Exception(string.Format("Unable to receive data from Uri: {0}", uri.ToString()));

                // write the results
                using (var write = File.Create(filePath))
                using (var read = await result.Content.ReadAsStreamAsync())
                {
                    read.Seek(0, SeekOrigin.Begin);
                    read.CopyTo(write);
                }

1 个答案:

答案 0 :(得分:0)

如果响应是长时间通话并且您想要增加超时,只需在客户端上设置它:https://msdn.microsoft.com/en-us/library/system.net.http.httpclient.timeout(v=vs.118).aspx

同样从课堂上看:

TimeSpan defaultTimeout = TimeSpan.FromSeconds(100.0);
TimeSpan maxTimeout = TimeSpan.FromMilliseconds((double) int.MaxValue);
TimeSpan infiniteTimeout = TimeSpan.FromMilliseconds(-1.0);