I have a static class with a static get property, and in this property, I do this:
// property body
{
// HttpContext.Current is NOT null
...
Parallel.ForEach(files, file =>
{
// HttpContext.Current is null
var promo = new Promotion();
...
});
...
// HttpContext.Current is NOT null
}
This static class doesn't undergone type initialization until a view uses this property.
The problem is that Promotion
's static constructor, which is initialized the first time a new Promotion()
is created within the Parallel.ForEach()
, uses HttpContext.Current
. When promo
is instantiated within the scope of this Parallel.ForEach()
, HttpContext.Current
is null
, and new Promotion()
therefore causes an exception.
HttpContext.Current
is not null within the static get property because it's not called until the view uses it (and there is therefore a HttpContext.Current
).
If Promotion
used HttpContext.Current
in its instances instead of its static members, I could probably just pass HttpContext.Current
into the new Promotion()
constructor:
var context = HttpContext.Current;
Parallel.ForEach(files, file =>
{
var promo = new Promotion(context);
});
But since static
members of Promotion need HttpContext.Current, I can't. I could probably redesign the Promotion
class to change the static members that need it to be instance members, but they are static for a reason--there would be a large performance penalty if all the members that were static had to be defined instead on each instance each time a new Promotion
was instantiated.
What are the possible workarounds for this? I didn't realize HttpContext.Current
would be null within the scope of Parallel.ForEach()
.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on.
Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".
noun plural c's, C's or Cs. the third letter and second consonant of the modern English alphabet. a speech sound represented by this letter, in English usually either a voiceless alveolar fricative, as in cigar, or a voiceless velar stop, as in case.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With