Solved

How to use external app.config in assembly?

Posted on 2013-06-25
7
1,025 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now