Hook into Application_Start in a HttpModule
Asked Answered
W

6

10

I’m implementing a simple HttpModule, where I want some code to run when the web application is started. But I’m surprised to find that the Application_Start event I would normally use from Global.asax is not available from a HttpModule. Is that correct, or am I missing something here?

How do I hook into the Application_Start event from an HttpModule?

Update:
I’ve come to simple solution using the Init event instead, but it still smells a bit funny to me.

Wasting answered 6/5, 2010 at 7:4 Comment(0)
B
29

You CAN use HttpModule to handle application start event

Contrary to others that only write/believe what they read I've done my own part and found out it's possible to handle Application start using an HTTP module. It's a bit of a hack really but it reliably works. It's definitely not something someone should avoid, because I've seen it in MS modules as well (namely Sharepoint 2010 SPRequestModule) This blog post of mine (Writing a custom IHttpModule that handles Application_Start event) will get you all the information you need to know about this. I've done it myself and it simply works. But you have to be a bit careful when using common resources, because your application may start behaving weird. To avoid this I suggest you read an additional blog post of mine, that explains why this happens and how to avoid it.

If you want it to be thread safe as well, you can also lock execution and then mark module as application started. It's the safest way of doing it.

private static bool isStarted = false;
private static object moduleStart = new Object();
...
if (!isStarted)
{
    lock(moduleStart)
    {
        if (!isStarted)
        {
            // handle aplication start
            ...
            isStarted = true;
        }
    }
}

I've created my own library that hooks to existing applications like Sharepoint 2010. I don't want to change Global.asax of Sharepoint now do I? Using technique explained in the blog post, I was able to hook into it. Easily.

And I guess this is exactly what you've been looking for. Hooking into start event of an arbitrary application by adding a module into web.config. Do it this way. It will work.

Brushwork answered 6/10, 2010 at 8:15 Comment(2)
@downvoter: May I know why you downvoted this answer? Maybe we can discuss details that you're unsatisfied with and my answer could be improved?Brushwork
This doesn't actually handle Application_Start, it simply emulates it. This certainly works for most situations I can think of.Quintana
G
1

You cannot attach to the Application_Start event in an HttpModule. Here's a list of available events.

Gracegraceful answered 6/5, 2010 at 7:7 Comment(3)
Hmm, that's what I feared. Do I have any other options for creating a reusable library that can be deployed on existing web applications by simply adding a dll and a few web.config changes?Wasting
What is this reusable library supposed to do?Gracegraceful
It sends out a notification that the web application has started.Wasting
S
1

I agree with Darin.

the reason being that the application needs to be loaded in order to load modules, so how can you execute code within module before the application is ready to begin loading the module itself?

What are you trying to do? Might be worth evaluating what the idea of your solution looks like :)

Hope this helps :)

Sententious answered 17/5, 2010 at 13:59 Comment(1)
Shouldn't there be a "I'm ready" event at least then, which fires when all the modules have been loaded and the app is up and running? I was trying to send out a notification to a remote server to indicate the site had been (re-)loaded.Wasting
A
1

I got interested in this thread and how a website started while fixing an bug in an old ASP.NET site.

So I put together a demo, to see how it would work. Seems the order IS from the web.config.

You can see here https://github.com/jradxl/MVC-Website-Without-Global.asax.cs It implements Robert Koritnik solution - thanks

Allaround answered 23/3, 2017 at 18:45 Comment(0)
W
0

As a matter of fact, there is, and always will be only one instance of specific http module until application pool recycle. And, of course, per w3wp.exe worker of you web application.

In other words, use init method or constructor to do initialisation, preload data etc. ;) and don't use static fields except you need to do locking once requests start firing events your module subscribed to and you need change data your module controls or contains as properties. Anyhow, init method is called on application start.

Wrong answered 28/10, 2015 at 21:53 Comment(0)
A
0

How does one know whether the Init() with the Lock code will be the first of the modules to get called? Surely other modules might be instantiated first? Isn't that the difference for Global.asax's Application_Start event - that it's guaranteed to be first to be called by design?

Allaround answered 23/3, 2017 at 2:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.