19

During practice of customizing VirtualPathProvider, I found that it the custom VirtualPathProvider can be registered in Global.asax or in AppInitialize method according to MSDN http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx. However, MSDN doesn't clearly describe the method AppInitialize.

Does any static AppInitialize method in App_code folder will be automatically invoked by ASP.NET runtime at start up?

Ben Laan
  • 2,607
  • 3
  • 29
  • 30
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230

1 Answers1

36

While there is precious little documentation about the AppInitialize() method, you are correct in your assumption that any class in your App_Code folder that contains a method signature like this:

public static void AppInitialize()

will be invoked when the Asp.Net application starts up. Remember that App_Code is a special folder to Asp.Net and everything inside there is treated a little differently. Good luck finding documentation on all the little quirks (like the aforementioned) of the App_Code folder.

Another thing to remember however is that only one class can contain a signature for the AppInitialize() method or else you will get a compiler error at runtime similar to this:

The AppInitialize method is defined both in 'App_Code.SomeClassOne' and in 'App_Code.SomeClassTwo'.

So while this is perfectly valid:

public class SomeClassOne
{
    public static void AppInitialize()
    {
        HostingEnvironment.Cache["InitializationTimeOne"] = DateTime.Now;
    } 
}

This will generate the compiler error I mentioned above:

public class SomeClassOne
{
    public static void AppInitialize()
    {
        HostingEnvironment.Cache["InitializationTimeOne"] = DateTime.Now;
    } 
}

public class SomeClassTwo
{
    public static void AppInitialize()
    {
        HostingEnvironment.Cache["InitializationTimeTwo"] = DateTime.Now;
    } 
}

I hope this clears things up a bit for you :)

Josh
  • 44,706
  • 7
  • 102
  • 124
  • This is very useful. Any idea of where to find other examples of App_Code special behaviors? – Dinis Cruz Jan 13 '13 at 12:01
  • 2
    I had this working in a WCF Webservice for some time, and suddenly it mysteriously stopped working. If I run the Webservice in Visual Studio the initialization takes place, but if I deploy it to IIS no initialization seems to take place. I could swear this was working, and now it suddenly isn't. Any ideas? – Farinha Oct 02 '13 at 16:21
  • Found the problem. I changed the "Build Action" on the .cs file that contained the initialization from "Content" to "Compile" in an attempt to not have the "App_Code" folder copied to the Webservice publish folder. Well, that can't be done... – Farinha Oct 03 '13 at 08:44
  • 1
    I would probably make the case that you shouldn't be using the App_Code folder for a WCF project. Instead you might want to look at the WCF way of doing things by creating your own custom ServiceHostFactory, so you can control what things get run during initialization. A pretty comprehensive example can be found here: http://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx – Josh Oct 03 '13 at 17:30
  • @Josh does that not seem like an over complication if you just want an "AppStart" method entry point. What is the benefit? – Paul C Sep 09 '14 at 09:29
  • 3
    @CodeBlend - This is a pretty old answer that pre-dates the more recent AppStart pattern. Most of the special ASP.Net folders are avoided these days, and the next version of ASP.Net uses a completely different model. – Josh Sep 09 '14 at 10:37
  • This does not work in case of WCF, I have hosted WCF in IIS, I am using my custom host to start service like below <%@ ServiceHost Language="C#" Debug="true" Factory="Namespace.UnityServiceHostFactory" Service="Service1" CodeBehind="Service1.svc.cs" %> – Ashish Shukla Feb 12 '19 at 11:32