C# 在任务工厂内分配HttpContext.Current是否正确?
在任务工厂中调用的方法中,我将HttpContext.Current设置为null。因此,我将HttpContext.Current赋值给currentContext变量。然后,我使用相同的变量来分配HttpContext.CurrentC# 在任务工厂内分配HttpContext.Current是否正确?,c#,C#,在任务工厂中调用的方法中,我将HttpContext.Current设置为null。因此,我将HttpContext.Current赋值给currentContext变量。然后,我使用相同的变量来分配HttpContext.Current var currentContext = HttpContext.Current; Task shipmentCreationCompleted = Task.Factory.StartNew(() => { Ht
var currentContext = HttpContext.Current;
Task shipmentCreationCompleted = Task.Factory.StartNew(() =>
{
HttpContext.Current = currentContext;
MethodToPerformSomeAction();
});
它现在运行良好,没有任何问题。如果我的代码在技术上有任何问题,请告诉我。或者有其他方法来处理这个问题吗?最后我根据评论使用了这样的方法
Task shipmentCreationCompleted = Task.Factory.StartNew(currentContext =>
{
HttpContext.Current = (HttpContext)currentContext;
MethodToPerformSomeAction();
}, HttpContext.Current);
它工作得很好 将HttpContext作为参数传递给任务工厂,那么您将始终使用与调用方法相同的HttpContext谢谢alikuli。如果我使用它会有什么区别?您将使用调用任务正在使用的请求的完全相同的引用。没有不相容性。基本上这是参数注入。由于您的方法有效,可能该方法也很好,但在多用户环境中会发生很多事情,因此我觉得传递HttpContext的引用并使用它更安全。这样我就知道了我要对付的动物。