Configuration File Question

  • Thread starter Thread starter Ian
  • Start date Start date
I

Ian

I have an application called "Test.exe" and this application has a config
file called "Test.exe.config" and it holds the connection string for the
database calls in there. To get the conectionstring out of the config file
the following lines of code are utilized.

Private settings As New System.Configuration.AppSettingsReader
Private o As Object = settings.GetValue("ConnectionString", GetType(String))
Private strConnection As String = o.ToString()

This works fine.... But I also have an exception assembly that I imported
into this application "Test.exe". The assembly is called
"CallExceptionHandling.dll" and it also has a config file called
"CallExceptionHandling.dll.config" and inside sits the connectionstring that
pertains to the generic exception handler assembly. The code used in the
assembly to get the connectionstring is the same code as used for the
application.

But when I run the application and the exception assembly goes and gets the
connectionstring it is looking to the "Test.exe" config file and not the dll
config file.

Where am I going wrong?

Thanks
 
Hello Ian,

Thanks for your post. I'd like to share the following information with you:

1. Generally speaking, DLLs do not have config files in a .NET application.
Rather, they share the configuration information of the EXE that is being
used. Because the AppDomainSetup.ConfigurationFile specifies the EXE.Config
by default. The AppSettingsReader class reads the application settings when
the default AppDomain is loaded, and then those settings are cached for the
lifetime of the AppDomain.

2. Based on my experience and research, there are two methods to work
around this problem:

Method 1
========
I strongly recommend you create a custom Application Configuration class
which loads the configuration from another file. The following code snippet
demonstrate load a .Config file and deserialize XML to get the value:

//-----------------code snippet-------------------
// Create an XmlSerializer for the ApplicationSettings type.
mySerializer = new XmlSerializer(typeof(ApplicationSettings));
FileInfo fi = new FileInfo(Application.LocalUserAppDataPath
+ @"\CallExceptionHandling.dll.config");
// If the config file exists, open it.
if(fi.Exists)
{
myFileStream = fi.OpenRead();
// Create a new instance of the ApplicationSettings by
// deserializing the config file.
ApplicationSettings myAppSettings =
(ApplicationSettings)mySerializer.Deserialize(
myFileStream);
// Assign the property values to this instance of
// the ApplicationSettings class.
String myConnectionString = myAppSettings.ConnectionString;
}
//----------------end of ----------------------

I believe the article below is very helpful:

Persisting Application Settings in the .NET Framework
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/persistappsettnet.asp

Method 2
========
Another method is to create a new AppDomain with the
AppDomainSetup.ConfigurationFile set to "CallExceptionHandling.dll.config",
and run the dll in the new AppDomain.

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top