C#: Adding context to Parallel.ForEach() in ASP.NET

后端 未结 4 1843
甜味超标
甜味超标 2020-12-16 00:42

I have a static class with a static get property, and in this property, I do this:

// property body
{
    // HttpContext.Current is NOT null
    ...

    Par         


        
相关标签:
4条回答
  • 2020-12-16 00:57

    As Mauricio points out, the HttpContext.Current depends on the currently executing thread. It strikes me as unusual that a static constructor depends on the such an inherently transient value as HttpContext.Current, but perhaps that was not your idea.

    If you can change the Promotion class, that would be the first option I'd consider.

    If not, you need somehow to force type initialization for Promotion at a point where HttpContext.Current is still valid. To learn what forces type initialization read this Jon Skeet blog post.

    An option could be to create a dummy Promotion object, (just once in the entire program should be enough). If that is not an option, you could try by reading the property with reflection. I don't know if that forces type initialization, but I guess so.

    0 讨论(0)
  • 2020-12-16 01:06

    Just pass any context you have from outside the Parallel.ForEach call down into any functions you call inside that rely on said context.

    var context = HttpContext.Current;
    Parallel.ForEach(items, item =>
        {
            DoSomething(item, context);
        }
    );
    
    
    
    private static void DoSomething(item, context = null) {
        if (context == null) context = HttpContext.Current;
    
        ...
    }
    

    I like to have fallback to null so I don't have to worry about passing around the context all the time. I only make sure to remember that my functions need context when calling from another thread, and then I slap that baby right in there.

    0 讨论(0)
  • 2020-12-16 01:16

    It does not work, because inside the foreach, a new thread is created, so the context is null. Even creating a method DoSomething to set the curren context, the context is still null.

    0 讨论(0)
  • 2020-12-16 01:18

    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)

    In a nutshell: you need to break out of using HttpContext.Current statically.

    0 讨论(0)
提交回复
热议问题