ASP.NET Webforms IHttpModule 单例

发布于 2024-08-12 01:57:55 字数 367 浏览 1 评论 0原文

我有一个类在与网站不同的程序集中实现 IHttpModule。该模块实现拦截请求并重写网站的 URL。

映射存储在具有请求的 url 和目标 url 的类中。

第二个示例是 MTSingleton,来自 http://devhood.com/Tutorials/tutorial_details。 aspx?tutorial_id=486 适合创建映射列表吗?模块实现中是否有更好的方法?

编辑:我的错,这是针对 IIS 6.0 和 .NET 3.5 SP1

I have a class that implements IHttpModule in a separate assembly from a website. The module implementation intercepts requests and rewrites urls for the website.

The mappings are stored in a class with the requested url and the destination url.

Is the second example, MTSingleton, from http://devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=486 suitable for creating the mapping list? Is there a better approach from within the module implementation?

Edit: My bad, this is for IIS 6.0 and .NET 3.5 SP1

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

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

发布评论

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

评论(1

最美的太阳 2024-08-19 01:57:55

听起来您希望在应用程序周期中创建一次映射对象。听起来您正试图阻止根据请求一遍又一遍地创建此内容。 (如果我错了,请澄清。)

查看 IHttpModule。假设您正在使用 IIS 7.0,ASP.Net 生命周期将显示 Init() 方法被触发一次。这意味着,每个应用程序生命周期都会触发一次。因此,启动 Web 服务器,第一个请求将启动 Init(),然后后续请求不需要启动它,直到刷新 Web 服务器应用程序周期。

您应该能够安全地将映射创建代码移动到 Init() 方法中,该方法应该为您提供您正在寻求的多线程单例类型初始化的保护措施。您应该仍然在映射对象周围有多线程保护措施,但是 IHttpModule 的 Init() 方法应该为您提供您正在寻求的一次完成的效果。

Sounds like you're looking to create the mappings object once in your app cycle. It sounds like you're trying to prevent this from being created over and over per request. (Please clarify if I'm wrong.)

Look at the methods on IHttpModule. Assuming you're working with IIS 7.0, the ASP.Net lifecycle will show that the Init() method is fired once. Meaning, it's fired once per application lifecycle. So, fire up the web server, first request will kick Init() into gear, then subsequent requests don't need to fire it until the web server application cycle is refreshed.

You should be able to safely move your mappings creation code into the Init() method, which should provide you with the safeguards you're seeking with a multi-threaded singleton type of initialization. You should still have multi-threaded safeguards around your mapping object, but the IHttpModule's Init() method should give you the fire-once-and-done effect you're seeking.

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