Solved

How to use external app.config in assembly?

Posted on 2013-06-25
7
1,050 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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