问题
We are currently seeing an issue with the use of HttpContext.Current.Items
where the local environments of the developers show no issues (all works as expected) in the server environment we are suddenly loosing items placed inside (getting a NullReferenceException
).
I sketched some code and use below:
Global.asax we initialise the factory at BeginRequest:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Items["Key"] = new Factory();
}
Inside the BaseControl
we have a property to retrieve the factory easily:
public Factory Factory
{
get { return HttpContext.Current.Items["Key"] as Factory; }
}
In the UserControl
we use it through the base property:
protected void Page_Load(object sender, EventArgs e)
{
Product p = Factory.CreateProduct();
}
Environment information:
- Local DEVs are running on Windows 7 and 8 using IIS 7.5 and 8 and Sitecore 6.6
- The server is running Windows Server 2008 R2 using IIS 7.5 and Sitecore 6.6
For all local DEVs (we're working on this project with 6 people) there's no issue with the code described above. However once we deploy the code to the test server the locations that use the Factory break (ea the HttpContext.Current.Items
is empty)
回答1:
I can imagine only 1 scenario when it behaves like you described: in the Global.asax
file the Inherits
property on the test server points to the Sitecore.Web.Application
directly omitting your code execution.
Could you double check if the Global.asax
file starts with
<%@Application Language='C#' Inherits="My.Assembly.Namespace.Global" %>
This could happen if the Global.asax
was changed in your dev enironment but hasn't been deployed to the test environment.
If it's not an issue, try to check if the Application_BeginRequest
is executed on the test server. It would give you an answer whether the Factory
is never added to HttpContext.Current.Items
or whether it's removed from the Items
during the request handling.
回答2:
I noticed you use the same name for your property as it's type:
public Factory Factory {}
Maybe this initiates some unexpected behavior?
来源:https://stackoverflow.com/questions/15948262/asp-net-httpcontext-items-vanish-on-server