<p>I have some ASP.NET web services which all share a common helper class they only need to instantiate one instance of <em>per server</em>. It's used for simple translation of data, but does spend some time during start-up loading things from the web.config file, etc. <em>The helper class is 100% thread-safe. Think of it as a simple library of utility calls. I'd make all the methods shared on the class, but I want to load the initial configuration from web.config.</em> We've deployed the web services to IIS 6.0 and using an Application Pool, with a Web Garden of 15 workers.</p>

<p>I declared the helper class as a Private Shared variable in Global.asax, and added a lazy load Shared ReadOnly property like this:</p>

<pre><code>Private Shared _helper As MyHelperClass

Public Shared ReadOnly Property Helper() As MyHelperClass
    Get
        If _helper Is Nothing Then
            _helper = New MyHelperClass()
        End If
        Return _helper
    End Get
End Property
</code></pre>

<p>I have logging code in the constructor for <code>MyHelperClass()</code>, and it shows the constructor running for each request, even on the same thread. I'm sure I'm just missing some key detail of ASP.NET but MSDN hasn't been very helpful.</p>

<p>I've tried doing similar things using both <code>Application("Helper")</code> and <code>Cache("Helper")</code> and I still saw the constructor run with each request.</p>

[{'answer_id': 62913, 'author': 'Donny V.', 'author_id': 1231, 'author_profile': 'https://Stackoverflow.com/users/1231', 'pm_score': 0, 'selected': False, 'text': "<p>I 'v done something like this in my own app in the past and it caused all kinds of weird errors.\nEvery user will have access to everyone else's data in the property. Plus you could end up with one user being in the middle of using it and than getting cut off because its being requested by another user.</p>\n\n<p>No there not isolated.</p>\n"}, {'answer_id': 62924, 'author': 'JRoppert', 'author_id': 6777, 'author_profile': 'https://Stackoverflow.com/users/6777', 'pm_score': 2, 'selected': False, 'text': '<p>You can place your Helper in the Application State. Do this in global.asax:</p>\n\n<pre><code>  void Application_Start(object sender, EventArgs e)\n  {\n    Application.Add("MyHelper", new MyHelperClass());\n  }\n</code></pre>\n\n<p>You can use the Helper that way:</p>\n\n<pre><code>  MyHelperClass helper = (MyHelperClass)HttpContext.Current.Application["MyHelper"];\n  helper.Foo();\n</code></pre>\n\n<p>This results in a single instance of the MyHelperClass class that is created on application start and lives in application state. Since the instance is created in Application_Start, this happens only once for each HttpApplication instance and not per Request.</p>\n'}, {'answer_id': 63006, 'author': 'user7375', 'author_id': 7375, 'author_profile': 'https://Stackoverflow.com/users/7375', 'pm_score': 1, 'selected': True, 'text': "<p>It's not wise to use application state unless you absolutely require it, things are much simpler if you stick to using per-request objects. Any addition of state to the helper classes could cause all sorts of subtle errors. Use the HttpContext.Current items collection and intialise it per request. A VB module would do what you want, but you must be sure not to make it stateful.</p>\n"}]
['https://Stackoverflow.com/questions/62588', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6897/']