挂钩到 HttpModule 中的 Application_Start

发布于 2024-09-01 00:49:20 字数 259 浏览 4 评论 0原文

我正在实现一个简单的 HttpModule,我希望在 Web 应用程序启动时运行一些代码。但我惊讶地发现我通常从 Global.asax 使用的 Application_Start 事件在 HttpModule 中不可用。这是正确的吗,还是我在这里遗漏了一些东西?

如何从 HttpModule 挂钩 Application_Start 事件?

更新:
我已经使用 Init 事件找到了简单的解决方案,但它对我来说仍然有点有趣。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浮生面具三千个 2024-09-08 00:49:20

您可以使用 HttpModule 来处理应用程序启动事件

与其他只写/相信他们所读内容的人相反,我已经完成了自己的部分,并发现可以使用 HTTP 模块来处理应用程序启动。这确实有点麻烦,但它确实有效。这绝对不是人们应该避免的事情,因为我也在 MS 模块(即 Sharepoint 2010 SPRequestModule)中看到过它,我的这篇博客文章 (编写处理 Application_Start 事件的自定义 IHttpModule)将为您提供您需要了解的所有信息。我自己做过,而且很有效。但是在使用公共资源时必须小心一点,因为您的应用程序可能会开始表现得很奇怪。为了避免这种情况,我建议您阅读 其他博客文章 我的,这解释了为什么会发生这种情况以及如何避免它。

如果您也希望它是线程安全的,您还可以锁定执行,然后将模块标记为应用程序已启动。这是最安全的做法。

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

我已经创建了自己的库,可以连接到现有的应用程序(例如 Sharepoint 2010)。我现在不想更改 Sharepoint 的 Global.asax 吗?使用博客文章中解释的技术,我能够融入其中。容易地。

我想这正是您一直在寻找的。通过将模块添加到 web.config 中来挂钩任意应用程序的启动事件。这样做。它会起作用的。

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.

情深已缘浅 2024-09-08 00:49:20

您无法附加到 HttpModule 中的 Application_Start 事件。以下是可用事件列表

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

不忘初心 2024-09-08 00:49:20

我同意达林的观点。

原因是需要加载应用程序才能加载模块,那么如何在应用程序准备好开始加载模块本身之前执行模块内的代码呢?

你想做什么?
可能值得评估您的解决方案的想法:)

希望这有帮助:)

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 :)

遥远的绿洲 2024-09-08 00:49:20

我对这个线程以及修复旧 ASP.NET 站点中的错误时如何启动网站感兴趣。

所以我制作了一个演示,看看它是如何工作的。
似乎订单来自 web.config。

你可以在这里看到
https://github.com/jradxl/MVC-Website-Without- Global.asax.cs
它实现了 Robert Koritnik 解决方案 - 谢谢

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

少钕鈤記 2024-09-08 00:49:20

事实上,在应用程序池回收之前,特定 http 模块总是只有一个实例。当然,还有您的 Web 应用程序的每个 w3wp.exe 工作人员。

换句话说,使用 init 方法或构造函数进行初始化、预加载数据等。;)并且不要使用静态字段,除非一旦请求开始触发模块订阅的事件并且需要更改模块控制的数据或包含作为属性。无论如何,init 方法在应用程序启动时被调用。

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.

秋千易 2024-09-08 00:49:20

如何知道带有 Lock 代码的 Init() 是否将是第一个被调用的模块?当然其他模块可能会首先实例化?
这不是 Global.asax 的 Application_Start 事件的区别吗 - 它保证是第一个被设计调用的?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文