How to handle an application launch event in an ASP.NET module

I am writing an asp.net HTTP module that must read configuration data once from a local file (for example, config.xml stored in the root directory of the application), and then, based on the configuration, do some processing of incoming requests.

Since there are no Application_Start / Application_init connections in Asp.NET modules, what would be the best way to handle this scenario. I try to avoid reading the configuration file every time a request arrives. Ideally, I want to read the configuration file when the application starts.

I need to encode this only in the http module and not use Global.asax

+3
source share
5

. , - -

static string test; 
        public void Init(HttpApplication application)
        {


            application.BeginRequest +=(new EventHandler(this.Application_BeginRequest));
            test = "hi"; 
            application.EndRequest +=(new EventHandler(this.Application_EndRequest));


        }
       private void Application_BeginRequest(Object source,EventArgs e)
        {
            {
                HttpApplication application = (HttpApplication)source ;
                HttpContext context = application.Context;
                context.Response.Write(test);
            }


        }
0

, - ...

public MyConfig Config
{
    get
    {
        MyConfig _config = Application["MyConfig"] as MyConfig;
        if (_config == null)
        {
            _config = new MyConfig(...);
            Application["MyConfig"] = _config;
        }
        return _config;
    }
}

, Config ...

int someValue = Config.SomeValue;

,

, , Session["MyConfig"] Application["MyConfig"]

+1

, , init.

0

init httpmodule .

:

public void Init(HttpApplication context)
    {

        context.PostRequestHandlerExecute += (sender, e) =>
        {
            Page p = context.Context.Handler as Page;
            if (p != null)
            {
            ///Code here    
            }
        };
    }
0
public SomeHttpModule : IHttpModule
{    
    private static readonly Configuration Configuration = 
            ConigurationReader.Read();    
}
0
source

Source: https://habr.com/ru/post/1751773/


All Articles