I'm using a library that relies on HttpContext.Current. The library is Facebook C# SDK, but my question should apply in other scenarios as well. I'd like to use this library from inside of a parallel thread. However, HttpContext.Current is not available in the parallel thread, so I'm thinking about caching it to a local variable and then setting it in the parallel thread like this:
var httpContext = HttpContext.Current;
Parallel.ForEach(items, item => {
try {
HttpContext.Current = httpContext;
// Call a method that relies on HttpContext.Current
} finally {
HttpContext.Current = null;
}
});
Do you foresee anything wrong with this? Are there any repercussions to doing this?