2

I want to update web.config just once when application is started. For this I thought I can use application_start method in Global.asax. While application_start is called only once normally when the first request is made to website, it is being called for each http request when I use either System.Web.Configuration.WebConfigurationManager or Microsoft.Web.Administration.ServerManager to update web.config. Example code with WebConfigurationManager is here:

protected void Application_Start(object sender, EventArgs e)
    {
        Configuration config =WebConfigurationManager.OpenWebConfiguration(null);
        config.AppSettings.Settings.Remove("MyVariable");
        config.AppSettings.Settings.Add("MyVariable", "MyValue");
        config.Save(); 

        // Add event to event log to monitor when this method is called
        string sSource= "TryApplicationStart";
        string sLog= "Application";
        string sEvent= "Sample Event"; 

        if (!EventLog.SourceExists(sSource))
            EventLog.CreateEventSource(sSource, sLog);
        EventLog.WriteEntry(sSource, sEvent);
        EventLog.WriteEntry(sSource, sEvent,
            EventLogEntryType.Warning, 234);

    }

You can create an empty asp.net web application, add this code to global.asax and host it in IIS. Then refresh the page a couple of times and see in event log that there are events registered for each refresh.

Why is application_start being called for each request when config file is updated in this way? How can I update web.config sections once the application is started and not during each request?

tubakaya
  • 457
  • 4
  • 15
  • 1
    Changing the web.config file of an ASP.NET application causes the application to restart. This is by design - it looks like you've basically created an infinite loop. – Tim Oct 07 '15 at 16:05
  • 1
    That is how web applications hosted in IIS are designed to behave. Changes to the web.config are detected by the app pool, forcing the app pool to recycle. Once the app pool recycles, the application will respin back up, calling Application_Start – David L Oct 07 '15 at 16:05
  • 1
    If you want to store some sort of configuration at runtime, store it in an internal thread-safe structure or an external config file (such as a json config file) – David L Oct 07 '15 at 16:06
  • It's not clear why you want to change the web.config only once per application start, but I recommend you explore other ways of saving that value (for whatever purpose), as the current way simply won't work. – Tim Oct 07 '15 at 16:06
  • I see. Yes, indeed it just goes into an infinite loop. – tubakaya Oct 07 '15 at 16:13
  • Thank you both for answering – tubakaya Oct 07 '15 at 16:15
  • Guys, I need to add https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/weblimits#c on my application, how I can able to achieve it? I added on application_start and it goes to an infinite loop. any suggestion? – Ravish Patel Nov 11 '19 at 10:26

0 Answers0