Solved

How to use external app.config in assembly?

Posted on 2013-06-25
7
1,095 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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