Things to remember
- Connection strings get "scoped" to the dll via the name attribute.
- Other settings get "scoped" to the dll via their own section in the applicationSettings part of the config file.
- You have to setup that other section (for #2) at the top of the file.
- You may need to do a Search/Replace in all the dll source code: search for "My.MySettings" and replace with "My.Settings" (I've had to do this every time)
Below is a generic web.config file with the modifications for passing settings to a dll. The web app is "myWeb" and the dll is "myDll."
- In configSections, notice that we establish space for the configuration section at the bottom of the file.
- In the connectionStrings section, notice that the settings for myDll are set in the same section as the connectionStrings for the web app.
- Notice the new section near the bottom, in the applicationSettings section: myDll.My.Settings - that's where the non-connectionString settings are set for the dll.
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="myWeb.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- creates an area for myDll settings -->
<section name="myDll.My.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings>
<!-- this is accessible via Configuration.ConfigurationManager.AppSettings(key)-->
<add key="web_config_appSettings_tempFileLocation" value="c:\temp\" />
</appSettings>
<connectionStrings>
<add name="someConnectionString"
connectionString="Data Source=localhost;Initial Catalog="pathToMy.MDF";Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="myWeb.My.MySettings.someConnectionString1"
connectionString="Data Source=localhost;Initial Catalog="pathToMy.MDF";Integrated Security=True"
providerName="System.Data.SqlClient" />
<!-- scope the setting for the connectionstring here with the other connectionstrings -->
<add name="myDll.My.Settings.someConnectionString"
connectionString="Data Source=localhost;Initial Catalog="pathToMy.MDF";Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<customErrors mode="Off" />
<authentication mode="None" />
<compilation debug="true" strict="false" explicit="true" />
<pages>
<namespaces>
<clear />
<add namespace="System" />
<add namespace="System.Collections" />
<add namespace="System.Collections.Specialized" />
<add namespace="System.Configuration" />
<add namespace="System.Text" />
<add namespace="System.Text.RegularExpressions" />
<add namespace="System.Web" />
<add namespace="System.Web.Caching" />
<add namespace="System.Web.SessionState" />
<add namespace="System.Web.Security" />
<add namespace="System.Web.Profile" />
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.WebControls" />
<add namespace="System.Web.UI.WebControls.WebParts" />
<add namespace="System.Web.UI.HtmlControls" />
</namespaces>
</pages>
</system.web>
<applicationSettings>
<myWeb.My.MySettings>
<setting name="uploadPath" serializeAs="String">
<value>c:\uploads</value>
</setting>
<setting name="homeURL" serializeAs="String">
<value>http://127.0.0.1/</value>
</setting>
<setting name="isDebugMode" serializeAs="String">
<value>True</value>
</setting>
</myWeb.My.MySettings>
<!-- area for myDll settings -->
<myDLL.My.Settings>
<setting name="emailServer" serializeAs="String">
<value>mail.somename.com</value>
</setting>
<setting name="emailFrom" serializeAs="String">
<value>user@somename.com</value>
</setting>
</myDLL.My.Settings>
</applicationSettings>
</configuration>
1 comment:
Excellent article! It really helped me out!
Post a Comment