5
\$\begingroup\$

Here's the issue. When I build an asp.net application I create a singleton to read the values from the web.config file so that the values are only read once and gives a slight speed increase to the app. I basically do the following :

/// <summary>
/// Static class that handles the web.config reading.
/// </summary>
public class SiteGlobal
{
    /// <summary>
    /// Property that pulls the connectionstring value.
    /// </summary>             
    public static string ConnectionString { get; protected set;}

    /// <summary>
    /// Property that pulls the directory configuration value.
    /// </summary>
    public static string Directory { get; protected set; }

    /// <summary>
    /// Static contructor for the SiteGlobal class.
    /// </summary>
    static SiteGlobal() {
        ConnectionString = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        Directory = WebConfigurationManager.AppSettings["Directory"];

    }
}

Which I don't consider a bad optimization. (Suggestions for improvement are appreciated through.)

Now though I'm trying to read from a ConfigurationSection that can contain multiple area's for example :

<ApplicationArea>
    <test1>
        <add key="key value" value="value data"/>            
    </test1>
    <test2>
        <add key="key value1" value="value data2"/>
    </test2>
</ApplicationArea>

Where I can have multiple area's underneath ApplicationArea, such as test1 and test 2 above. The best I've been able to come up with for an optimization of this though is this :

public static string ApplicationArea(string skey, string skey2)
    {
        NameValueCollection nvc = WebConfigurationManager.GetSection("ApplicationArea/" + skey) as NameValueCollection;
        return nvc[skey2];                        
    }       

Which is not saving the data and rereads the web.config file every time it's accessed. Is there an easy way to optimize the reading of the sections?

\$\endgroup\$
4
  • 2
    \$\begingroup\$ The web.config is loaded into memory once per-request anyway - it isn't loaded and parsed every time you access a section via the WebConfigurationManager. What speed increases have you actually noticed? \$\endgroup\$ May 4 '11 at 19:01
  • \$\begingroup\$ In fact, it might not even occur for each request, according to MSDN: 'These settings are calculated once and then cached across subsequent requests. ASP.NET automatically watches for file changes and re-computes the cache when any of the configuration files change within that file's hierarchy. When the server receives a request for a particular URL, ASP.NET uses the hierarchy of configuration settings in the cache to find the requested resource.' So, you're just doubling up on memory consumption; and I'm not convinced it's faster. \$\endgroup\$ May 4 '11 at 19:10
  • \$\begingroup\$ So, my static optimizations were not needed and the reading of the sections should be fine. Thanks. \$\endgroup\$
    – mwgriffith
    May 4 '11 at 20:21
  • \$\begingroup\$ @Mr. Disappointment: IIS does load web.config once and registers changes made directly to the web.config while the application is loaded. If there are changes, it will reload it. This only applies to the web.config file though. If you have an element in the web.config that references an external .config, changes in the external file will not register and the settings will remain static until the application pool is recycled. \$\endgroup\$ May 5 '11 at 13:54
8
\$\begingroup\$

The web.config is loaded into memory once per-request anyway - it isn't loaded and parsed every time you access a section via the WebConfigurationManager. What speed increases have you actually noticed?

In fact, it might not even occur for each request, according to MSDN: 'These settings are calculated once and then cached across subsequent requests. ASP.NET automatically watches for file changes and re-computes the cache when any of the configuration files change within that file's hierarchy. When the server receives a request for a particular URL, ASP.NET uses the hierarchy of configuration settings in the cache to find the requested resource.' So, you're just doubling up on memory consumption; and I'm not convinced it's faster.

\$\endgroup\$
2
  • \$\begingroup\$ I posted Mr. Disappointment's comments as a community wiki answer to that others can see that the question was addressed. \$\endgroup\$
    – Michael K
    May 5 '11 at 14:02
  • \$\begingroup\$ I had thought about doing so, just so it has some resolution; apologies for not getting around to it, and thanks. \$\endgroup\$ May 5 '11 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.