1

I have been developing an ASP.Net 4.0 web application, and I have used the HttpApplicationState class with its Contents property to set some variables in the Application_Start event of the Global_asax file.

While the application runs as expected on my development machine, when it is deployed on the production server running IIS7 the values returned for the application state variables are null. I have tried various code techniques, like using the HttpContext,Runtime.Cache, the Application["key"], and the Application.Contents.Add["key",Value], all with no success. I am wondering now whether there should be a special configuration on IIS7 on this; researching the topic I see that people say that the application pool in which the application is executed must be served by only a single worker process; I am no IIS7 expert, but selecting the application pool, and viewing its advanced properties, I can see that the pool is configured to have a maximum of 1 worker process, so I believe this should cover it. I have spent a good 2 days on this, so if anyone has any knowledge of this issue, please help!

thanks

chris

user1012598
  • 417
  • 1
  • 8
  • 18

2 Answers2

1
  1. HttpContext.Current.Application is available in both IIS 6 and 7, integrated mode or classic.

  2. The Application_Start event in global.asax is also called in all environments.

  3. When running IIS 7+ in integrated mode, event handlers in global.asax are only applied to requests that are mapped to the ASP.NET handler (objects derived from the Page class). Custom HttpModules are applied to all requests.

  4. Application state, or any other static variables, are not shared from one instance of IIS to another -- so settings in one AppPool or worker process aren't visible in others.

  5. Application state and post-init static variables settings are lost across AppPool resets (though you will also get a new Application_Start event). This is true whether the resets are due to the worker being idle or for the usual daily resets.

  6. Instead of using HttpApplicationState, it's better to create your own static class, with static properties for app-wide settings. That way, your accesses are strongly typed, have Intellisense, and aren't susceptible to misspelling key strings.

  7. Whether using your own static class or HttpApplicationState, be sure to set a lock before reading a setting when you might also change it. You can use HttpApplicationState.Lock() if you decide to stay with that approach.

You might try running your app under IIS Express locally, or even IIS 7 (Vista) or 7.5 (W7), to help debug your issue. Cassini is deprecated at this stage; I would avoid using it as much as possible.

RickNZ
  • 18,448
  • 3
  • 51
  • 66
  • Thanks for your input; so following 6. above where is a safe place to create the static class which can be accessed by all other classes in the application? Would the application's root directory be a good choice? BTW I only bought your book "Ulra-Fast ASP.NET" last week! – user1012598 Dec 05 '11 at 08:29
  • In a web application can be almost anywhere; just try to keep it in the same namespace as the bulk of your app. For a web site, it should be in the App_Code folder. I hope you enjoy the book. – RickNZ Dec 06 '11 at 01:37
0

It is probably that you do not have the correct application pool mode.

IIS 7 application pools have 2 modes, classic and integrated. See. http://weblogs.asp.net/jgaylord/archive/2008/09/04/iis7-integrated-mode-and-global-asax.aspx

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • thanks, i switched mode to classic; the variables seem to now have values; however the page is served with no style information! is there something one can do with classic mode to serve the application as normal? – user1012598 Nov 27 '11 at 18:43
  • I have not seen that effect of switching between classic and integrated before. You could check your deployment that you have all files. Also see http://stackoverflow.com/questions/716049/what-is-the-difference-between-classic-and-integrated-in-iis7 for the difference between the modes. – Shiraz Bhaiji Nov 27 '11 at 18:58
  • thanks for this; your hint out me into looking at the possible correlation between the Application Pool mode and the use of Application State; it appears that the Integrated mode does not access the Application_Start event; however trying to run the web site under classic mode application pool resulted on ages not being sent correctly (no style information was sent). So back to square one I am afraid; due to lack of time, I had to retreat into using the Session state instead (pretty wasteful if you ask me); if anyone has any idea on how to deal with this thing please let me know. – user1012598 Nov 28 '11 at 14:36