ConfigurationManager.AppSettings Caching

47

Solution 1

A quick test seems to show that these settings are only loaded at application startup.

//edit the config file now.
Console.ReadLine();

Console.WriteLine(ConfigurationManager.AppSettings["ApplicationName"].ToString());
Console.WriteLine("Press enter to redisplay");

//edit the config file again now.
Console.ReadLine();
Console.WriteLine(ConfigurationManager.AppSettings["ApplicationName"].ToString());
Console.ReadLine();

You'll see that all outputs remain the same.

Solution 2

Try it,

ConfigurationManager.RefreshSection("appSettings")

Just be careful file name (in bin folder)

Normal file name : appname.exe.config

if debug mode : appname.vshost.exe.Config

Solution 3

It doesn't matter if it does or not. Don't fix a performance problem if there isn't one.

Solution 4

It reads the application configuration file (MyApp.exe.config) once at application startup, as can easily be verified by changing the file while the app is running.

The comment in the forum post referenced by the OP was:

The values for the Web.config are stored into cache/memory when the application starts hence why the app restarts when any changes are made to the web.config. Note that this only applies to the Web.config, any other .config files you may use are accessed from the disk by default

I would interpret this comment as meaning that config files other than web.config in an ASP.NET application are accessed from the disk by default. And similarly, config files other than MyApp.exe.config in a WinForms/Console application are accessed from the disk by default.

This comment is not stating that MyApp.exe.config is read from the disk by default.

Solution 5

AppSettings is cached. You can improve performance by further caching to limit namevaluecollection lookups.

See: DotNetPearls Static Config Pattern

Share:
47

Related videos on Youtube

Takács Zoltán
Author by

Takács Zoltán

Updated on June 21, 2020

Comments

  • Takács Zoltán
    Takács Zoltán almost 4 years

    Is this a good way, to delete all the selected rows from sql table?

    The code works fine, but maybe there other or better ways to do this. The real_ escape_string function is in the good place, or i should put in in the foreach loop, before the sql query?

    $('.deleteRows').click(function(e)
    {
        e.preventDefault();
        var val = [];
        if(confirm("Biztos, hogy törölni szeretné a kijelölt sorokat?"))
        {
            $(':checkbox:checked').each(function(i){
              val[i] = $(this).val();
            });
            $.ajax({
                data: { val:val },
                type: 'POST',
                url: 'files/delete_all_uzenet.php',
                success: function(data) 
                {
                    var result = $.trim(data);
                    $('#newsletterResult').html(data);
                    $('#newsletterModal').modal('show');
                },
                complete: function()
                {
                    setTimeout(function() 
                    {
                        location.reload();  
                    }, 4000 );  
                }
            });
        }
        return false;
    });
    

    Php file:

    <?php
    include_once("../../files/connect.php");
    if(isset($_POST['val']))
    {
        foreach($_POST['val'] as $v)
        {
            mysqli_query($kapcs, $sql = "DELETE FROM kapcsolatfelvetel WHERE kapcsolat_id = '".mysqli_real_escape_string($kapcs, $v)."'") or die(mysqli_error($kapcs));
        }
        echo 'Rows deleted';
    }
    else
    {
        exit("No rows selected.");
    }
    ?>
    
    • RiggsFolly
      RiggsFolly almost 8 years
      No not really, use prepared parameterised queries to avois SQL Injection
  • Dave Markle
    Dave Markle over 15 years
    I somewhat disagree here. Configuration values like this are likely to be used in all kinds of places in your application, including nested loops and such. Grokking whether the read of a config file will take microseconds vs. milliseconds is important to know.
  • JamesUsedHarden
    JamesUsedHarden over 14 years
    Just to be picky, settings get loaded the first time they're referenced not necessarily at application startup.
  • Oskar Austegard
    Oskar Austegard about 12 years
    To be even more picky (and off topic to boot) - no need to call ToString() - it's already a string
  • heymega
    heymega about 10 years
    You just needed to check the System.Configuration.ConfigurationManager class and you'll see that the class and its properties are static
  • Luis Perez
    Luis Perez almost 9 years
    You will have to call ConfigurationManager.RefreshSection("appSettings") in order to get the changes. You could also add a file watcher to reload it only when it changes.
  • Takács Zoltán
    Takács Zoltán almost 8 years
    Thanks, i will check it tomorrow. $p = &$_POST['val'] - What does the &-character mean? And...$values = '('.implode(','$p).')'; In the implode, why you writed $p? Not $v?
  • BeetleJuice
    BeetleJuice almost 8 years
    @TakácsZoltán & sets $_POST and $p to the same reference in memory. Without it, $p would just be a copy, meaning if you add a value to one, it is not added to the other. With it, they refer to the same thing by different names, so changing one also changes the other. As for your 2nd question, look at the documentation for implode. It takes the array that has the values you want to glue together. So I used $p since that's the array.
  • Takács Zoltán
    Takács Zoltán almost 8 years
    I get sytax error for this: $values = '('.implode(','$p).')';
  • BeetleJuice
    BeetleJuice almost 8 years
    @TakácsZoltán I was missing a comma just before $p. I've added it.
  • Takács Zoltán
    Takács Zoltán almost 8 years
    And before deleting, how can i check, that the rows(ID-s) are realy in the table?
  • BeetleJuice
    BeetleJuice almost 8 years
    @TakácsZoltán You got it; When there is an error, mysqli_query() returns false, so doing mysqli_query(...) or die(mysqli_error(...)) will abort and show any error. If the code gets to the next line, then you know the query executed successfully. If you use my code as the basis for the solution to your initial question, remember to upvote and select my answer. Happy coding.
  • ruffin
    ruffin over 3 years
    Why would one access in anything but a static constructor, then? (assuming a watch isn't added) Also -- @SamuelNeff Are all settings read on first reference, or are you saying different settings are read from the same file at different times? If it's the latter, I'm starting to wonder about our friends in Redmond.