Eric J.
Eric J.

Reputation: 150228

applicationSettings and Web.config

I have a DLL that provides logging that I use for WebForms projects and now wish to use it in an ASP.Net MVC 2 project.

Some aspects of that DLL are configured in app.config:

<configuration>
    <configSections>
            <section name="Tools.Instrumentation.Properties.Settings" 
                     type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                     requirePermission="false" />
        </sectionGroup>
    </configSections>

 <applicationSettings>
        <Tools.Instrumentation.Properties.Settings>
            <setting name="LogLevel" serializeAs="String">
                <value>DEBUG</value>
            </setting>
            <setting name="AppName" serializeAs="String">
                <value>MyApp</value>
            </setting>
            <setting name="Port" serializeAs="String">
                <!--value>33333</value-->
                <value>0</value>
            </setting>
        </Tools.Instrumentation.Properties.Settings>
    </configuration>    

However, when I create a similar entry in Web.config, I get the error:

Unrecognized configuration section applicationSettings

My two-part question:

Upvotes: 24

Views: 38778

Answers (3)

Sam
Sam

Reputation: 42427

Here's the .NET 4 version of the missing configuration:

<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="Tools.Instrumentation.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>

Make sure to update the namespace of the <section>'s name attribute value to match your own.

Upvotes: 6

xr280xr
xr280xr

Reputation: 13302

Your config file was just missing the applicationSettings section group:

<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
        <section name="Tools.Instrumentation.Properties.Settings" 
                 type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                 requirePermission="false" />
    </sectionGroup>
</configSections>

If you add that, you can put your Settings section inside the tag and your assembly should read from it as normal.

Upvotes: 64

Sky Sanders
Sky Sanders

Reputation: 37104

Notice the name attribute of the section?

try removing your element from the <applicationSettings> wrapper

   <Tools.Instrumentation.Properties.Settings>
        <setting name="LogLevel" serializeAs="String">
            <value>DEBUG</value>
        </setting>
        <setting name="AppName" serializeAs="String">
            <value>MyApp</value>
        </setting>
        <setting name="Port" serializeAs="String">
            <!--value>33333</value-->
            <value>0</value>
        </setting>
    </Tools.Instrumentation.Properties.Settings>

Now you can use the section. But you do not have the generated wrapper class you you will need to do a little more work to get your values using ConfigurationManager.

As to the second part of your question, from one perspective, there is little to no difference in the way that configuration files are treated by a web application vs a forms applications.

The one salient difference that may or may not be relevant here is the way that web.config files can be hierachially mapped, each subsequent file effectively augmenting or modifying the parent configuration, when allowed. But this is more of a behavioral difference as opposed to a functional difference, in my opinion.

Upvotes: 3

Related Questions