简体   繁体   中英

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:

  • How do I make this config entry work in Web.config ?
  • Where can I read up on the conceptual differences between WinForms configuration and ASP.Net configuration?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM