SyntaxHighlighter

Thursday, December 17, 2015

Merging DLL project Settings with WebApp web.config

Recently I was working on MVC Web API project and needed to store key / value pairs. I decided to use <applicationsettings> because it is strongly typed and compiler auto-generates Settings class with each key as property. But I wanted to create Settings in referenced DLL project with external XML file to hold configuration items. As DLL can’t have their own .config file, I have to merge settings to Web application web.config file. This post describes steps to merge settings DLL project settings with Web application web.config file.

Creating new Settings was easy. Open DLL project Properties, click on “Settings” tab, click Create new settings link. New file will be created under Properties folder named: Settings.settings and App.config will be updated as follows:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="{Project Namespace}.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <{Project Namespace}.Properties.Settings>
      <setting name="MyFirstSetting" serializeAs="String">
        <value>1234</value>
      </setting>
    </{Project Namespace}.Properties.Settings>
  </applicationSettings>
</configuration>

Step 1 – Externalize setting file in DLL project


This can done by updating App.config file as below.
...
<applicationSettings>
    <{Project Namespace}.Properties.Settings configSource="serviceSettings.config" />
</applicationSettings>
Create new xml file “serviceSettings.config” (same folder as App.config) and move all settings to this new file.
<{Project Namespace}.Properties.Settings>
  <setting name="MyFirstSetting" serializeAs="String">
    <value>1234</value>
  </setting>
</{Project Namespace}.Properties.Settings>
At this point you have created external settings file.

Step 2 – Merge with web.config


Open web.config and copy following fragment from App.config to web.config under <configuration> >> <configSections> node.
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="{Project Namespace}.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
And add these line under <configuration> node.
<applicationSettings>
  <{Project Namespace}.Properties.Settings configSource="serviceSettings.config" />
</applicationSettings>
And copy “serviceSettings.config” file from DLL project to Web application (same folder as web.config)

Finally you are done. You can now access strongly typed Settings in your DLL project.
{Project Namespace}.Properties.Settings.Default.MyFirstSetting
To automate copy operation, you can write post build command to move serviceSettings.config file from DLL project to Web application project folder.
  
Hope this helps someone.

Thanks,
-Javed

2 comments: