HttpContext的是空在一个叫里面的Parallel.For方法 [英] HttpContext is null in a method called inside Parallel.For
问题描述
想了很多后,发布了这个问题。这样做正常为
是不是一种选择,因为我们需要做在很短的时间大量的处理。
Posting this question after trying a lot. Doing normal for
is not an option because we need to do a large amount of processing in very less time.
我 GetDataFor()
这里面 HttpContext.Current
被使用。
在code是这样的:
public void SomeMethod()
{
var context = HttpContext.Current;
Parallel.For(0, 100, i =>
{
var data = GetDataFor(i, context);
});
}
public data GetDataFor(int i, HttpContext context)
{
Uri requestUri = null;
if (HttpContext.Current != null)
{
requestUri = HttpContext.Current.Request.Url;
sCookie = string.Format("{0}", HttpContext.Current.Request.Headers["cookie"]);
}
else
{
requestUri = context.Request.Url;
}
//do something
return data;
}
一切工作正常里面罚款
循环。然而,当我把它里面的 的Parallel.For
并通过 HttpContext.Current
, HttpContext.Current.Request
, HttpContext.Current.Request.Url
参数:
Everything works fine inside normal for
loop. However, when I call it inside Parallel.For
and pass HttpContext.Current
, HttpContext.Current.Request
, HttpContext.Current.Request.Url
as method parameters:
HttpContext.Current无法序列化,因为它没有参数的构造函数
HttpContext.Current cannot be serialized because it does not have a parameterless constructor
传递 HttpContextBase的HttpContext = NULL
作为参数抛出:
Passing HttpContextBase httpContext = null
as parameter throws:
要成为XML序列化,它自ICollection继承的类型必须在其继承层次结构的各个层次添加的实现(System.Object的)。 System.Web.HttpApplicationStateBase没有实现添加(System.Object的)。
To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. System.Web.HttpApplicationStateBase does not implement Add(System.Object).
试图使一个属性:
public string[] httpContextData
{
get
{
string requestUrl = HttpContext.Current.Request.Url.ToString();
string sCookie = string.Format("{0}", HttpContext.Current.Request.Headers["cookie"]);
return new string[] { requestUrl, sCookie };
}
}
和使用方法:
var contextData = httpContextData;
这抛出:
的System.Uri不能被序列化,因为它没有参数的构造函数
System.Uri cannot be serialized because it does not have a parameterless constructor
我做了所有该向它发送的<一个href=\"http://stackoverflow.com/questions/8109526/the-cross-thread-usage-of-httpcontext-current-property-and-related-things#comment58652454_12219078\">reference和<一个href=\"http://stackoverflow.com/questions/7851532/c-anything-wrong-with-setting-httpcontext-current-in-a-parallel-thread#comment58653226_7851532\">state但无法理解为什么问题没有解决。
I did all this to send it's reference and state but unable to understand why the problem is not solving.
我如何使用 HttpContext.Current
在的Parallel.For
?我在做什么错在这里?
How do I use HttpContext.Current
inside Parallel.For
? What am I doing wrong here?
顺便说一句,需要的东西有:
Btw, the needed stuff are:
HttpContext.Current.Request.Url
和 HttpContext.Current.Request.Headers [曲奇]
推荐答案
HttpContext.Current
才可用(NOT NULL)的请求处理线程<内部/ EM>。 的Parallel.For
创建多个线程,其中没有一个是可以访问 HttpContext.Current
。
HttpContext.Current
is only available (not null) inside request-handling threads. Parallel.For
creates multiple threads, none of which is has access to HttpContext.Current
.
您必须通过
- 循环或之前分配的局部变量
-
TLocal
中使用实例的Parallel.For&LT; TLocal方式&gt;
- local variables assigned before the loop or
TLocal
instance used inParallel.For<TLocal>
.
在任何情况下,code,如 HttpContext.Current ....
出去了。
In any event, code such as HttpContext.Current....
is out.
这篇关于HttpContext的是空在一个叫里面的Parallel.For方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!