Search code examples
asp.net.netvb.netiis-8iis-10

Why are my .NET application variables disappearing on my web server?


Background

Application/Program: lightweight website (40,000 HTML pages | 5 .ASPX pages).

Goal: use application variables (.NET) ~ via Global.asax

Problem: web server is failing to use application variables (yet dev environment is fine)

Desire: web server uses application variables

Tech Specs

  • Development environment: IIS 10.0 (IIS Express)
  • Production environment: IIS 8.0 (web server)
  • Server-side: .ASPX webforms; Visual Basic
  • Connection/communication: one server/domain (ie, no web farms; no web gardens)

Details

  1. In my development environment, my application variables are working fine (ie, 100% success rate)

  2. On my web server, though, my application variables are failing (0% success rate)

Code - Global.asax.vb:

Sub Application_Start(sender As Object, e As EventArgs)
  Application("fooBar") = "Hello, World"
End Sub

MyPage.aspx:

<script runat="server">
  Protected Function EmbedMe() As String
    Return Application("fooBar")
  End Function
</script>


<nav><%: EmbedMe() %></nav>

Troubleshooting

So far, I've tried to solve my problem in the following ways:

  1. Adding/removing my Global.asax file [on the web server]
  2. Adding/removing certain project files[on the web server] (eg,obj*; packages*`; etc.)
  3. Restarting the IIS Web Site on my web server
  4. Examining the application pool on my web server (I haven't made any changes, though)
  5. Using a different IIS Web Site on my web server
  6. Researching this problem (on StackOverflow.com; on Google.com; on Bing.com; etc.)

Request

May you please help me get my web server to respect/use my .NET application variables?

Thanks


Solution

  • REAL SOLUTION

    Place the following three files onto the webserver:

    1. ~/bin/App_global.asax.compiled
    2. ~/bin/App_global.asax.dll
    3. ~/PrecompiledApp.config

    REAL ISSUE

    The real problem was that my Web Server was not executing Application_Start(). The reason it wasn't firing was, apparently, because my Web Server was not running my Global.asax file.


    REAL PROBLEM

    My web server was missing three files:

    1. ~/bin/App_global.asax.compiled
    2. ~/bin/App_global.asax.dll
    3. ~/PrecompiledApp.config

    Reason: I presume that it was missing those files because I don't use VS2019's Publish functionality. Instead, I just: (i) compile my website application; and (ii) copy my files onto my webserver (via a [selective] batch file).


    REAL WARNING/DISCLAIMER

    You will probably need to recycle the ApplicationPool on your WebServer!! For me – despite adding those files – my web server [still] wouldn't fire the Application_Start() routine (and, thus, create my Application Variables) until I recycled my ApplicationPool


    ¹ small note: apparently, it doesn't matter if those three [aforementioned] files are very outdated. My files had timestamps from 4 years ago, yet all of my updates were working.
    ² smaller note: apparently, the web server doesn't need these three files in order to execute your .aspx and/or .html files properly.


    SUMMARY

    Just copy those three files onto your webserver and you should start seeing your Application Variables on your website/webapp (all [other] things being equal, of course).

    I hope this can help you.

    Best Wishes.