Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use external app.config in assembly?

Posted on 2013-06-25
7
Medium Priority
?
1,179 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 2000 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
Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

 

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 To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

704 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