勾成的Application_Start在HTTP模块 [英] Hook into Application_Start in a HttpModule

查看:146
本文介绍了勾成的Application_Start在HTTP模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现一个简单的HTTP模块,在这里我想要一些code到Web应用程序启动时运行。但我惊讶地发现,Application_Start事件我通常会从使用的Global.asax从HTTP模块不可用。那是正确的,还是我失去了一些东西?

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?

我如何挂钩到Application_Start事件从一个HttpModule?

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

更新:结果
我来使用Init事件,而不是简单的解决方案,但它仍然闻起来有点好笑给我。

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

推荐答案

相反,其他人只写/相信他们读什么我做我自己的一部分,并发现它可以处理应用程序启动使用HTTP模块。这是一个黑客位真的,但它工作可靠。这绝对不是一个人应尽量避免,因为我已经在MS模块看到它,以及(即SharePoint 2010的取值prequestModule )这个矿的博客文章(<一个HREF =htt​​p://erraticdev.blogspot.com/2010/08/writing-ihttpmodule-thats-able-to.html>编写处理Application_Start事件一个自定义的IHttpModule)将让你的所有信息,你需要了解这一点。我已经做到了我自己,它只是工作。但是,你必须使用公共资源的时候,因为你的应用程序可能会开始行为怪异小心一点。为了避免这种情况我建议你阅读的额外的博客文章矿山,这可以解释为什么发生这种情况以及如何避免它。

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;
        }
    }
}

我已经建立了我自己的图书馆挂接像2010年我的Sharepoint现有的应用程序并不想改变的SharePoint的Global.asax现在做的吗?在使用博客文章解释技术,我可以挂接到它。轻松。

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.

和我想这正是你一直在寻找的东西。挂钩到任意应用程序的启动事件通过增加一个模块插入的web.config 。做这种方式。它将工作。

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.

这篇关于勾成的Application_Start在HTTP模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆