Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
287 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...