C#:在ASP.NET中添加内容到Parallel.ForEach() [英] C#: Adding context to Parallel.ForEach() in ASP.NET
问题描述
我有一个静态get属性静态类,并在这个属性,我这样做:
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.
问题是,促销
的静态构造函数,它初始化首次新推广()
在 Parallel.ForEach()
,使用内创建 HttpContext.Current
。当促销
是这个范围内的实例 Parallel.ForEach()
, HttpContext.Current
是空
和新推广()
因此导致异常。
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
不是静态get属性中无效,因为它不叫,直到视图使用它(并因此 HttpContext的存在。电流
)。
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
).
如果促销
使用 HttpContext.Current
在它的实例,而不是它的静态成员,我很可能只是通过 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);
});
促进
不过,由于静态
成员需要HttpContext.Current,我不能。我可能重新设计促销
类来改变这种状况需要它实例成员静态成员,但它们是静态的一个原因 - 会有一个大的性能损失,如果一切是静态的成员有每个新的促销
被实例化的时间,而不是被定义在每个实例上。
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.
什么是这种情况的可能的解决方法?哪知 HttpContext.Current
将范围内为空 Parallel.ForEach()
。
What are the possible workarounds for this? I didn't realize HttpContext.Current
would be null within the scope of Parallel.ForEach()
.
推荐答案
HttpContext.Current因为它是非web线程运行为null。如果您使用分叉新的Thread(...)的一些code
这将是完全一样的。该TPL有些隐藏这一点,但你仍然需要认识到,在每次迭代的 Parallel.ForEach
可以在不同的线程可能运行,并相应地对待它。
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.
在特定的,如果你想使用一些类或方法出来的Web请求(和Parallel.ForEach是这样的用法),你就不能使用HttpContext.Current。一种解决方法是明确地传递在构造函数中的HttpContext(或HttpContextBase改进可测试性)(或方法参数)
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)
在一言以蔽之:你需要摆脱使用HttpContext.Current的静态
In a nutshell: you need to break out of using HttpContext.Current statically.
这篇关于C#:在ASP.NET中添加内容到Parallel.ForEach()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!