Solved

How to use external app.config in assembly?

Posted on 2013-06-25
7
1,061 Views
Last Modified: 2013-07-02
Hi experts,

I have an external assembly and its config file in one directory. I have an other application, that loads and runs this asssembly using Reflection.
So far so good - that all works great.

This external dll uses its Proeprties.Settings for some sample settings. After changing the Settings in the dll's config file I recognized, that the Properties.Settings are still the same. My guess is: The data for the settings are stored inside the Proeprties.Settings class.

Here is my question: How to make the assembly  use its config file?

Thanks for taking care of this question,
kankman




Here is some code:

Sample Class of external assembly:
	public class MyClass : IApplicationSetting
	{
		public ApplicationSettingsBase GetSettings()
		{
			Properties.Settings.Default.Reload();
			return Properties.Settings.Default;
		}
	}

Open in new window



Programm part, that access the external assembly:
foreach (FileInfo file in sourceDirectory.GetFiles("*.dll"))
			{
				Assembly fileAssembly =  Assembly.LoadFile(file.FullName);

				Type type = fileAssembly.GetTypes().FirstOrDefault(t => !t.IsInterface && typeof (IApplicationSetting).IsAssignableFrom(t));

				if(type == null)
				{
					continue;
				}

				IApplicationSetting appSettings = fileAssembly.CreateInstance(type.FullName) as IApplicationSetting;

				ApplicationSettingsBase appSettingsBase = appSettings.GetSettings();

				foreach (SettingsProperty property in appSettingsBase.Properties)
				{
					Console.WriteLine("Name: {0}, Value: {1} ({2})", property.Name, appSettingsBase[property.Name],
									  property.Prope[embed=file 662482]rtyType.Name);
				}
			}

Open in new window


External assembly and config fileExecution result
0
Comment
Question by:kankman
  • 4
  • 3
7 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39277161
The app.config file loaded is always that in the directory of the application being loaded regardless of where the DLL is loaded. The config applies to the application and that is the way it should be.

To make it work for the application you are using to dynamically load the DLL from, you just add those property settings to the app.config for that app.
0
 

Author Comment

by:kankman
ID: 39283515
Hi Dale,

that's not, what I wanted to hear. I need to make the external assembly use its config file. Isn't there any other way?
The intention is, to use those assemblies as plugins. Just copy them into a specified folder and the main application loads them and you  can use them dynamically.
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 39283525
OK, so the point of an app.config file is to provide configuration for an application, not a library. Unless you have a very good reason to do otherwise you should include any settings required by a DLL in every app.config for any application using the DLL - even if those settings are identical.

Potentially you might create a custom section within the app.config file specifically for this DLL e.g. http://devlicio.us/blogs/derik_whittaker/archive/2006/11/13/app-config-and-custom-configuration-sections.aspx.

If you really don't like that there are two options that might be worth exploring. You can manually load a config file http://stackoverflow.com/questions/771814/how-to-read-app-config-from-a-custom-location-i-e-from-a-database-in-net but I think that will replace the currently loaded config file - you would have to play with that.

Or you can run your DLL inside an AppDomain which can have its own config file e.g. http://msdn.microsoft.com/en-us/library/system.appdomainsetup.configurationfile.aspx

However doing either of these is really using the config files in ways they were not intended. Its normal practice to have to include information in your existing app config file when adding a new library app.

If you aren't expecting the config values to change maybe you can embed them directly in your DLL? Or you could write something in your DLL to automatically add the relevant section to the config file of the app its being run under the first time its loaded? Although I'm not sure if that will take effect until the next time the app is loaded.

HTH
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:kankman
ID: 39292891
Hi Dale,

thanks a lot for your ideas. That all was things, I thought about. I now mixed some of them.

I wrote a manager, that loads the config of the the given assembly.

Here is the method the caller just calls at the end. He gives its Properties.Settings.Default Instance to it:

protected void RefreshJobAppConfig(ApplicationSettingsBase settings)
{
	if (settings == null)
		throw new ArgumentNullException("settings");
	if (!Jobdefinition.HasJobConfig)
	{
		return;
	}
	
	ApplicationSettingsManager appSettingsManager = new ApplicationSettingsManager(jobDefinition.JobType.Assembly);
	
	foreach (SettingsProperty source in appSettingsManager.Settings.Properties)
	{
		settings[source.Name] = appSettingsManager.Settings[source.Name];
	}
}

Open in new window


The manager is attached.

Thanks for giving me the knowledge, that there is normally no native working way.
ApplicationSettingsManager.cs
0
 

Author Closing Comment

by:kankman
ID: 39292897
Thanks for the information. That helped me out.
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39294681
No worries - I've spent a lot of time on this myself, so good it could be of help.
0
 

Author Comment

by:kankman
ID: 39295702
I also had an extra possibility to do the trick (but that was to many work): a partial class of Settigns with an default constructor that assigns a new settings provider, that loads data from the config file. (I just want to point in this possible solution).

Thanks again.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question