I've defined a customized HttpHandler to run my own web applications like this:
public interface IMyApp {
public void InitOnce();
public void Run();
}
public class MyApp1 : IMyApp {
public void InitOnce() {
// heavy-load some data on initializing
}
public void Run() {
}
}
//
// and there are MyApp2, MyApp3 .... MyAppN both implement IMyApp interface
//
public class MyHttpHandler : IHttpHandler, IRequiresSessionState {
public bool IsReusable { get; } = false;
public virtual void ProcessRequest(HttpContext ctx) {
var appID = ctx.Request.Params["appID"];
// create a fresh app instance depend on user request.
var app = (IMyApp)AppUtil.CreateInstance(appID);
/*
* TODO: I want some magics to make this method run only once.
*/
app.InitOnce();
app.Run();
}
}
As MyAppX instance will be created dynamically many times, I want to ensure that InitOnce() can only be processed once when MyApp1,2,3..N created at very first time. (just like putting InitOnce() in each of their static constructor)
Are there any talent ideas to do this? (try to avoid heavy locks if you can please)