4

I have a WPF project which has its own app.config file. I have also created a separate dll which my WPF project references. My dll has its own app.config too.

The issue I have is in my dll I have a section of code (below) which doesn't work in the WPF application. From reading around this is because the start-up project's app.config file is the default file so when I use the ConfiguationManager below it does not find any of the app settings so the strings are null. As it is looking in the WPF's config file rather than my dll config. What is the best way to get around this issue as I don't want to have to make a copy of these settings in another config.

public Messenger()
{                        
    _queueManagerName = ConfigurationManager.AppSettings["QueueManagerName"];
    _queueNameSendMessage = ConfigurationManager.AppSettings["QueueNameSendMessage"];
    _queueNameRecieveMessage = ConfigurationManager.AppSettings["QueueNameRecieveMessage"];
    _queueChannel = ConfigurationManager.AppSettings["QueueChannel"];
    _queueConnectionName = ConfigurationManager.AppSettings["QueueConnectionName"];
}

What is the best way to solve this problem? All I want is for my dll to read its own config file rather than the WPF config file.

Alexandre
  • 498
  • 2
  • 12
mHelpMe
  • 6,336
  • 24
  • 75
  • 150
  • This is problem already answered ... Check [**here**][1] [1]: http://stackoverflow.com/questions/4554980/app-config-problem – Moumit Nov 11 '13 at 14:36

4 Answers4

1

As referenced in this answer, A configuration file for a .Net .dll isn't standard practice, is not supported directly, and is generally not recommended.

Community
  • 1
  • 1
Claies
  • 22,124
  • 4
  • 53
  • 77
0

as you have said, the application running uses its config file - period. what you can do is use ConfigurationManager.OpenExeConfiguration : http://msdn.microsoft.com/en-us/library/ms224437(v=vs.110).aspx

does this help?

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
0

I think that you should use WPF config file. But if you want to have config data of your library in separate file, you can do it and reference this file from WPF config.

0

What you can do on your DLL is manually load the config, because a DLL cannot have configuration.

var FileMap = new ExeConfigurationFileMap();
FileMap.ExeConfigFilename = DllConfigFileName;
var cfg =  ConfigurationManager.OpenMappedExeConfiguration(FileMap, ConfigurationUserLevel.None);
var setting = cfg.AppSettings;
Luiz Felipe
  • 1,123
  • 8
  • 14