this method - doDayBegin(item.BranchId)
is taking long time to execute. So I am using Parallel.ForEach
to execute it parallel. When I am using norm
You get the error because you are trying to get the HttpContext
from a thread that is not running for the purpose of responding to a request.
The HttpContext.Current
property uses the thread to identify which context to get, as the web server can run several threads to handle requests. As the Parallel.ForEach
starts new threads, they will not be connected to a HttpContext
.
You need to pass along the information that the method needs in the call to the method.
HttpContext.Current is null because it's running in "non-web threads". If you forked some code using new Thread(...) it would be exactly the same. The TPL somewhat hides this, but you still need to realize that each iteration in your Parallel.ForEach can potentially run in a different thread, and treat it accordingly.
In particular, if you want to use some class or method out of the web request (and Parallel.ForEach is such an usage) you just can't use HttpContext.Current. A workaround is to explicitly pass the HttpContext (or HttpContextBase for improved testability) in the constructor (or as a method parameter)
example :
var context = HttpContext.Current;
Parallel.ForEach(items, item =>
{
DoSomething(context);
}
);
private static void DoSomething(HttpContext context) {
}
Further adding to Bayu Alvian answer. I had a similar problem and I solved it by passing the context as parameter but inside the method I got
Member 'method name' cannot be accessed with an instance reference
I solved it by doing a little tweak from the above answer.
// Get the new context
HttpContext context = HttpContext.Current;
Parallel.ForEach(items, item =>
{
DoSomething(context);
}
);
private static void DoSomething(HttpContext context) {
HttpContext.Current = context;
}
Assigning the context to the HttpContext.Current removes it.
HttpContext.Current
is set per thread. So when you fire up more threads using Parallel.ForEach
your new threads can't access it that way. The solution would be to pass the needed values as parameters all the way instead of relying on HttpContext.Current
in your repositories.
There are several sources here on SO that covers this problem already.
The cross-thread usage of "HttpContext.Current" property and related things
HttpContext.Current.Items in different thread
Access HttpContext.Current from different threads