2

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)

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
IvanL
  • 2,475
  • 1
  • 26
  • 39
  • perhaps an applicationpool restart? – JP Hellemons Apr 11 '13 at 12:03
  • @JPHellemons If it would be due to an applicationpool restart the entire page should not render, only the components that use the factory are returning errors. – IvanL Apr 11 '13 at 14:11
  • @IvanL have you tried `Application_AcquireRequestState`? I would suspect `Application_BeginRequest` is being invoked on all requests (even for CSS, javascript, etc) and `HttpContext.Current` may not be available in some cases. AquireRequestState happens later in the pipeline, and may provide more reliable access to `HttpContext.Current.Items`. Just a guess... – Derek Hunziker Apr 11 '13 at 17:52
  • @DerekHunziker I will give it a try, sadly we're on a tight shedule so for now we have created a workaround – IvanL Apr 13 '13 at 10:57
  • @IvanL did you find a solution to this problem? I'm having exactly the same issue – Mark_Gibson Dec 18 '14 at 11:28
  • @Webbie4 No we never resolved this issue and left the workaround in place – IvanL Jan 05 '15 at 08:34

2 Answers2

0

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.

Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
  • We verified this: The global.asax file inheritance is correct, we also did a Sitecore.Diagnostics.Log.Info and verified that the code is being executed. – IvanL Apr 11 '13 at 14:09
  • Can you try with the string variable passed to the `Items` instead of `Factory` instance? And also try logging `Request.RawUrl` for both `BeginRequest` method and `Factory` getter? – Marek Musielak Apr 11 '13 at 14:32
0

I noticed you use the same name for your property as it's type:

public Factory Factory {}

Maybe this initiates some unexpected behavior?

Martijn van der Put
  • 4,062
  • 1
  • 18
  • 26
  • No, the actual code has different class - property names, the shown names are simplified. – IvanL Apr 15 '13 at 13:23