HttpClient.GetAsync(...)在使用等待/异步时从不返回(HttpClient.GetAsync(…) never returns when using await/async)
编辑: 这个问题看起来可能是同一个问题,但没有回应...
编辑:在测试用例5中,任务似乎被卡在
WaitingForActivation
状态。我使用.NET 4.5中的System.Net.Http.HttpClient遇到了一些奇怪的行为 - 其中“等待”调用结果(例如)
httpClient.GetAsync(...)
将永远不会返回。这仅在使用新的异步/等待语言功能和Tasks API的某些情况下发生 - 代码总是似乎在仅使用延续时才起作用。
以下是一些重现问题的代码 - 将其放入Visual Studio 11中的一个新的“MVC 4 WebApi项目”,以显示以下GET端点:
/api/test1 /api/test2 /api/test3 /api/test4 /api/test5 <--- never completes /api/test6
这里的每个端点返回相同的数据(来自stackoverflow.com的响应头),除了
/api/test5
,从未完成。我在HttpClient类中遇到了错误,还是以某种方式滥用API?
复制代码:
public class BaseApiController : ApiController { /// <summary> /// Retrieves data using continuations /// </summary> protected Task<string> Continuations_GetSomeDataAsync() { var httpClient = new HttpClient(); var t = httpClient.GetAsync("http://stackoverflow.com", HttpCompletionOption.ResponseHeadersRead); return t.ContinueWith(t1 => t1.Result.Content.Headers.ToString()); } /// <summary> /// Retrieves data using async/await /// </summary> protected async Task<string> AsyncAwait_GetSomeDataAsync() { var httpClient = new HttpClient(); var result = await httpClient.GetAsync("http://stackoverflow.com", HttpCompletionOption.ResponseHeadersRead); return result.Content.Headers.ToString(); } } public class Test1Controller : BaseApiController { /// <summary> /// Handles task using Async/Await /// </summary> public async Task<string> Get() { var data = await Continuations_GetSomeDataAsync(); return data; } } public class Test2Controller : BaseApiController { /// <summary> /// Handles task by blocking the thread until the task completes /// </summary> public string Get() { var task = Continuations_GetSomeDataAsync(); var data = task.GetAwaiter().GetResult(); return data; } } public class Test3Controller : BaseApiController { /// <summary> /// Passes the task back to the controller host /// </summary> public Task<string> Get() { return Continuations_GetSomeDataAsync(); } } public class Test4Controller : BaseApiController { /// <summary> /// Handles task using Async/Await /// </summary> public async Task<string> Get() { var data = await AsyncAwait_GetSomeDataAsync(); return data; } } public class Test5Controller : BaseApiController { /// <summary> /// Handles task by blocking the thread until the task completes /// </summary> public string Get() { var task = AsyncAwait_GetSomeDataAsync(); var data = task.GetAwaiter().GetResult(); return data; } } public class Test6Controller : BaseApiController { /// <summary> /// Passes the task back to the controller host /// </summary> public Task<string> Get() { return AsyncAwait_GetSomeDataAsync(); } }
Edit: This question looks like it might be the same problem, but has no responses...
Edit: In test case 5 the task appears to be stuck in
WaitingForActivation
state.I've encountered some odd behaviour using the System.Net.Http.HttpClient in .NET 4.5 - where "awaiting" the result of a call to (e.g.)
httpClient.GetAsync(...)
will never return.This only occurs in certain circumstances when using the new async/await language functionality and Tasks API - the code always seems to work when using only continuations.
Here's some code which reproduces the problem - drop this into a new "MVC 4 WebApi project" in Visual Studio 11 to expose the following GET endpoints:
/api/test1 /api/test2 /api/test3 /api/test4 /api/test5 <--- never completes /api/test6
Each of the endpoints here return the same data (the response headers from stackoverflow.com) except for
/api/test5
which never completes.Have I encountered a bug in the HttpClient class, or am I misusing the API in some way?
Code to reproduce:
public class BaseApiController : ApiController { /// <summary> /// Retrieves data using continuations /// </summary> protected Task<string> Continuations_GetSomeDataAsync() { var httpClient = new HttpClient(); var t = httpClient.GetAsync("http://stackoverflow.com", HttpCompletionOption.ResponseHeadersRead); return t.ContinueWith(t1 => t1.Result.Content.Headers.ToString()); } /// <summary> /// Retrieves data using async/await /// </summary> protected async Task<string> AsyncAwait_GetSomeDataAsync() { var httpClient = new HttpClient(); var result = await httpClient.GetAsync("http://stackoverflow.com", HttpCompletionOption.ResponseHeadersRead); return result.Content.Headers.ToString(); } } public class Test1Controller : BaseApiController { /// <summary> /// Handles task using Async/Await /// </summary> public async Task<string> Get() { var data = await Continuations_GetSomeDataAsync(); return data; } } public class Test2Controller : BaseApiController { /// <summary> /// Handles task by blocking the thread until the task completes /// </summary> public string Get() { var task = Continuations_GetSomeDataAsync(); var data = task.GetAwaiter().GetResult(); return data; } } public class Test3Controller : BaseApiController { /// <summary> /// Passes the task back to the controller host /// </summary> public Task<string> Get() { return Continuations_GetSomeDataAsync(); } } public class Test4Controller : BaseApiController { /// <summary> /// Handles task using Async/Await /// </summary> public async Task<string> Get() { var data = await AsyncAwait_GetSomeDataAsync(); return data; } } public class Test5Controller : BaseApiController { /// <summary> /// Handles task by blocking the thread until the task completes /// </summary> public string Get() { var task = AsyncAwait_GetSomeDataAsync(); var data = task.GetAwaiter().GetResult(); return data; } } public class Test6Controller : BaseApiController { /// <summary> /// Passes the task back to the controller host /// </summary> public Task<string> Get() { return AsyncAwait_GetSomeDataAsync(); } }
原文:https://stackoverflow.com/questions/10343632
满意答案
尝试像这样格式化导入(而不是在媒体查询中):
@import url(foo.css) screen and (min-width:320px); @import url(bar.css) screen and (min-device-width:768px) and (orientation:portrait); @import url(blah.css) screen and (min-device-width:768px) and (orientation:landscape);
Try formatting your imports like this (instead of inside a media query):
@import url(foo.css) screen and (min-width:320px); @import url(bar.css) screen and (min-device-width:768px) and (orientation:portrait); @import url(blah.css) screen and (min-device-width:768px) and (orientation:landscape);
相关问答
更多定向平板电脑与手机(orientation tablet vs phone)
如何使用媒体查询分离手机与平板电脑肖像与平板电脑景观(how to use media query to seperate phone vs tablet portrait vs tablet landscape)
媒体查询:如何定位桌面,平板电脑和手机?(Media Queries: How to target desktop, tablet and mobile?)
如何检测React Native上的平板电脑风景(How detect tablet landscape on React Native)
iOS / Android Phonegap - 智能手机的肖像,平板电脑上的风景(iOS/Android Phonegap - portrait for smartphones, landscape on tablets)
整洁2.0平板电脑媒体查询不起作用(Neat 2.0 tablet media query not working)
Android平板电脑横向和纵向模式(Android Tablet Landscape and Portrait Modes)
如何在风景mdoe手机上运行我的Android平板电脑应用程序时看起来一样(How can I make my android tablet app to look the same when it runs on phone in landscape mdoe)
开发通用的Android应用程序(手机和平板电脑) - 定义方向(Developing an universal android application (phone and tablet) - Defining Orientations)
针对目标移动设备(第一),平板电脑和桌面的特定媒体查询(Specific media queries for target mobile (first), tablet & desktop)
相关文章
更多关于多线程问题,signalAll,await问题
微信公众平台.net HttpClient 异步客户端
httpclient get请求
Windows Phone 获取app在商店中的版本(检查app的版本号)
httpClient快速入门
httpclient post 请求
HttpClient DELETE请求示例
HttpClient CacheConfig缓存处理示例
httpclient 带参数 get 请求
Httpclient整合Spring教程
最新问答
更多python的访问器方法有哪些
使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
透明度错误IE11(Transparency bug IE11)
linux的基本操作命令。。。
响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
元素上的盒子阴影行为(box-shadow behaviour on elements)
它看起来像只在Windows上的Chrome的错误。 我在Google Canary (Chrome 63)中也进行了测试,问题依然存在,所以有可能它不会很快修复。 这个问题是由overflow: auto引起的overflow: auto ,在你的情况下,它可以很容易地通过删除或设置为可见(默认)来解决。 但是 ,将鼠标悬停在右侧(顶部和底部)时,会出现滚动条。 一个解决方案可以设置overflow: hidden的身体,所以预期的结果是所需的。 我想指出,这不是一个很好的解决方案,但我建议暂
Laravel检查是否存在记录(Laravel Checking If a Record Exists)
这取决于您是否要以后与用户合作,或仅检查是否存在。 如果要使用用户对象(如果存在): $user = User::where('email', '=', Input::get('email'))->first(); if ($user === null) { // user doesn't exist } 如果你只想检查 if (User::where('email', '=', Input::get('email'))->count() > 0) { // user found
设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
$scope.getData= function () { var reader = new FileReader(); reader.onload = $('input[type=file]')[0].files; var img = new Image(); img.src =(reader.onload[0].result); img.onload = function() { if(this.width > 640
Copyright ©2023
peixunduo.com All
Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!
Laravel检查是否存在记录(Laravel Checking If a Record Exists)
设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!