views:

345

answers:

2

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?
+2  A: 

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.

Sky Sanders