Friday, October 5, 2007

Configuring a .Net dll

I recently added a web-based application to an existing VB.Net application, and figured the best way to keep the business logic synchronized between the two applications would be to create a "business logic" dll that would be referenced by both the web and desktop applications. Everything worked well in the development environment, but as soon as I transitioned the application to IIS, it broke. Suddenly, it dawned on me that the "myDll.config" file wasn't being used. After reading through several posts, it became obvious that I needed to "include" the configuration for the dll in the myApp.config (for the desktop application) and myWeb.config (for the web application) files. Again I went a-Google-ing, only to find several posts, none of which explained exactly how to do it. So here's what I've learned, and what I did that works.

Things to remember

  1. Connection strings get "scoped" to the dll via the name attribute.
  2. Other settings get "scoped" to the dll via their own section in the applicationSettings part of the config file.
  3. You have to setup that other section (for #2) at the top of the file.
  4. 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=&quot;pathToMy.MDF&quot;;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="myWeb.My.MySettings.someConnectionString1"
connectionString="Data Source=localhost;Initial Catalog=&quot;pathToMy.MDF&quot;;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=&quot;pathToMy.MDF&quot;;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:

Anonymous said...

Excellent article! It really helped me out!