How to Get Web.config to Load Again

In .NET applications, we encounter 2 types of configuration files. I is "Web.config" which is used for configuring the settings for a Web application and the other is "App.config" used for Windows application settings.

Any changes made to the Spider web.config file causes a web awarding to restart the Awarding Domain. This behavior occurs by design. Different spider web applications, modifying the Application Configuration files for Windows Applications does not restart the application. The reason being that when the Windows Application runs, a new Awarding Domain is created for the application and it caches all the settings of the configuration file in temporary memory, and all the references to the settings volition be referred to the temporary memory cache and not to the actual file.

Let us see in practice how the configuration settings are used by a Windows application and how to change the settings at runtime without closing the application.

Create a Windows Forms awarding using Visual Studio 2005/2008/2010/2012 as below:

.net-new-project.gif

Figure: Creating a new project

Selecting-Windows-Forms.gif

Figure: Selecting Windows Forms

At present add together a button control on the Windows Forms class and name it "Prove Logging Flag" equally below:

Selecting-Windows-Forms1.gif

Figure: Calculation and modifying the text of a Button Control

Check if the awarding configuration file is available in the projection. If the application was created in Visual Studio 2012 and then the application configuration file is bachelor by default. Otherwise, add a new awarding configuration file.

Open the application configuration file and add the <appSettings> department as below:

  1. < appSettings >
  2. < add key = "LoggingFlag" value = "ON" />
  3. </ appSettings >

At present add a reference to System.Configuration in your project.

Adding-System.Configuration.gif

Effigy: Adding Arrangement.Configuration

Now add the code in the Form1.cs file to read the value from the configuration file and show it in a MessageBox equally below:

  1. using  System;
  2. using  System.Collections.Generic;
  3. using  System.ComponentModel;
  4. using  System.Information;
  5. using  Organization.Drawing;
  6. using  System.Linq;
  7. using  Organization.Text;
  8. using  System.Threading.Tasks;
  9. using  Organisation.Windows.Forms;
  10. using  System.Configuration;
  11. namespace  TestAppSettingRefresh
  12. {
  13. public  partial course  Form1 : Class
  14.     {
  15. public  Form1()
  16.         {
  17.             InitializeComponent();
  18.         }
  19. private void  button1_Click( object  sender, EventArgs due east)
  20.         {
  21.             MessageBox.Show(ConfigurationManager.AppSettings["LoggingFlag" ].ToString());
  22.         }
  23.     }
  24. }

Now run the application and check if the value is displayed in the MessageBox.

Adding-System.Configuration1.gif

Figure: Showing the value read from the configuration file. At present click "OK" and do not shut the application.

Go to the bin folder and open up the debug folder as below:

Debug-folder.gif

Figure: Bin\Debug folder

If you observe the files in the folder yous will find 2 configuration files. One is the with the naming convention ApplicationName.exe.config and the other i is ApplicationName.vhost.exe.config.

The file vhost is the hosting process and it supports the debugging and design-time expression evaluation. There is an article on this. Delight go through the link here for more than details. Then when an application is loaded the settings are loaded into the vhost.exe.config file and application reads the values from this file and caches the settings. So if you modify any values in the ApplicationName.exe.config file when the awarding is running (when you press CTRL+F5) you volition non run into the same settings updated in the ApplicationName.vhost.exe.config unless the application is restarted.

Now let us open the file "TestAppSettingRefresh.exe.config" and modify the value to "OFF" and save the file.

Modifying-web-config.gif

Figure: Modifying the value to OFF

Now click on the push again. The value is non changed in the MessageBox and it yet shows the value equally "ON".

Showing-value-in-a-MessageBox.gif

Figure: Showing value in a MessageBox

As a do good of the doubt, let us go and change the vhost.exe.config file. Past the style, check if irresolute the value in the exe.config file before had changed the value in vhost.exe.config file or not. Open the vhost.exe.config file at present.

The value is even so "ON". So, this tells us that unless the application is restarted the value will not change the value in the vhost.exe.config file.

  1. < appSettings >
  2. < add together fundamental = "LoggingFlag" value = "ON" />
  3. </ appSettings >

Now alter the value to "OFF" and click the button once more to see if the value is changed at present.

Modifying-web-config1.gif

Effigy: Irresolute the value to OFF

Yous will observe that still the value is shown as "ON". So, this proves that changing the values at runtime will not refresh the application settings and the awarding will nonetheless have the onetime values. In other words, the values are buried.

At present what nosotros are going to practice is to test if the vhost.exe.config file value changes if we build the application again past reading the values from the exe.config file. Let us see that first. So, change back the value to ON in vhost.exe.config file so that nosotros can examination it now. Close and run the awarding again. Now the application should show the value as "OFF" when the push button is clicked and also should the values in the vhost.exe.config file changes w.r.t exe.config file.

The value is shown equally "OFF" and even the vhost.exe.config value is refreshed with "OFF" at present:

Showing-value-in-a-MessageBox1.gif

Figure: Showing value is OFF

  1. < appSettings >
  2. < add together key = "LoggingFlag" value = "OFF" />
  3. </ appSettings >

Effigy: Settings in the vhost.exe.config file

And so now let us see how we can make the application to refresh itself so that information technology reads the modified values from the configuration file.

To practice that the ConfigurationManager has a method called RefreshSection(cord sectionName). This method refreshes the section specified in the method so that the application can read the inverse values.

Now your lawmaking should look similar the following:

refresh-web-config.gif

Figure: Refresh Section has now been added

Now run the application over again and alter the value to "False" in the TestAppSettingRefresh.exe.config file kickoff and check if the value is changed. It even so shows the one-time value. Now open up the file TestAppSettingRefresh.vhost.exe.config file and change the value to "False" and at present click the push button over again. This time it shows "False", the modified value.

This indicates that the application compiled in debug style reads the values from the vhost.exe.config and the mechanism to refresh the application settings value when the configuration file is modified is by calling ConfigurationManager's RefreshSection method.

Note: vhost files are there to provide support for debugging when you are running your application from Visual Studio by pressing the F5 push button. If you straight go to the Bin\Debug or Bin\Release folders and click on the .exe file and and then modify the configuration file, the value is refreshed as hither the application is running as an exe and non from the Visual Studio.

Now go to the Bin\Debug folder and cheque it. Click on the file TestAppSettingRefresh.exe and now go and change the value in the TestAppSettingRefresh.exe.config file and you lot will come across that the value is refreshed. From a applied point, nosotros deploy the application by packaging the application using Set up projects and the Setup projects e'er take the .exe file and .exe.config files for packaging and in that location will be no vhost files.

So, in this article, we have learned how to brand the Windows application read the modified values from the configuration file not from the Cache. The practical usage of this scenario is when you lot deploy a Windows Service into Product and programmatically modifying the configuration file value and when yous want your Windows Service to pick the new values. If you do not employ the RefreshSection method of the ConfigurationManager, you lot have to restart your Windows Service to pick upwardly the modified values.

Promise y'all liked this article. In the next commodity, we will discuss how to modify the configuration values programmatically and then that our application picks up the modified values.

sealshourgen.blogspot.com

Source: https://www.c-sharpcorner.com/UploadFile/akkiraju/refreshing-the-net-application-to-read-modified-configurati/

0 Response to "How to Get Web.config to Load Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel