Wilko van der Veen
Wilko van der Veen

Reputation: 456

Initializing ASP.NET Web application on application pool start

I have searched the web for over one day but I did not find anything I need.

I am developing an IIS web application with HttpHandlers and HttpModules. I need to initialize the application on first run (applying configuration). But I do not want to use global.asax.cs because I do not want implementations having a global.asax file in their folder (a web.config at most).

How do I run some code when the application pool is being initialized?

Upvotes: 1

Views: 376

Answers (1)

ddrjca
ddrjca

Reputation: 482

You could use the assembly level attribute PreApplicationStartMethodAttribute to have your startup code run early in the ASP.NET pipeline.

namespace MyWebService
{
     public class MyHttpHandler: IHttpHandler, IDisposable
     {

            public static void StartUp()
            {
                //Application Startup Code;
            }

            public void ProcessRequest(HttpContext context)
            {
                    //Do Something 
            }

            public bool IsReusable { get; private set; }

            public void Dispose(){};

            }
        }
    }
}

And add the attribute to your AssemblyInfo.cs

[assembly: PreApplicationStartMethod(typeof(MyWebService.MyHttpHandler), "StartUp")] 

For more info on PreApplicationStartMethod : https://msdn.microsoft.com/en-us/library/system.web.preapplicationstartmethodattribute

Upvotes: 1

Related Questions