HttpContext.Current為空匯總

1. async異步模式下為空

解决办法:

<httpRuntime targetFramework="4.5" />

或者:

In your appSettings, set aspnet:UseTaskFriendlySynchronizationContext to true.

2. 在非Http请求线程中使用为空

示例代码——将主线程的HttpContext传递进去:

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.html

posted @ 2017-09-13 13:45  _DN  阅读(187)  评论(0编辑  收藏  举报