解決辦法:html
<httpRuntime targetFramework="4.5" />
或者:app
In your appSettings, set aspnet:UseTaskFriendlySynchronizationContext to true.async
示例代碼——將主線程的HttpContext傳遞進去:pwa
var httpContext = HttpContext.Current; TaskWithDuration = new Task<long>(() => { var sw = new Stopwatch(); sw.Start(); HttpContext.Current = httpContext; action(); sw.Stop(); return sw.ElapsedMilliseconds; });
REF:線程
[HttpContext.Current並不是無處不在] http://www.cnblogs.com/fish-li/archive/2013/04/06/3002940.htmlcode