English 中文(简体)
ASP.NET Webforms IHttpModule Singleton
原标题:

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

最佳回答

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.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签