我刚刚开始学习 Task 的工作原理,并得到一个有趣的案例。我有HttpTaskAsyncHandler但如果我在 ProcessRequestAsync 中的代码调用某些任务,我将无法访问 HttpContext
public class MyAsyncHandler : HttpTaskAsyncHandler, IReadOnlySessionState
{
public override async Task ProcessRequestAsync(HttpContext context)
{
//can use HttpContext here
await MyJob("data");
//can use HttpContext here
}
public async Task MyJob(string data)
{
var func = Task.Factory.StartNew(() => Process(data));
await func;
}
public string Process(string context)
{
**//can't use HttpContext here**
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
}
有可能修复吗?我知道 Process 方法会在其他方面调用,但无论如何。谢谢/