2

Well, this question is a bit different from others with the slightly same title.

I add a config file to my DLL which will be used from a website and a console application. I'm testing the DLL from my web application.

When I build the DLL I can see my MyApp.dll.config in the bin\debug folder.

Nevertheless, when I try to read the settings from the DLL this way:

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);

The file is not found. I know it is something to do with the location where the application is being executed as Assembly.GetExecutingAssembly().Location return a path in Framework\v4.0.30319\Temporary ASP.NET Files\root\... while AppDomain.CurrentDomain.BaseDirectory return another completely different path.

So, what I'm doing wrong? There is some configuration missing to get the config file to be copied in the real location where the application is being ran?

Thank you in advance from your help!

sabotero
  • 4,265
  • 3
  • 28
  • 43
  • See this related question: https://stackoverflow.com/questions/34811381/visual-studio-msbuild-copy-referenced-class-librarys-app-config-as-dll-config – Jens Aug 06 '20 at 13:33

2 Answers2

3

Usually when you build a .dll and it has a config, that file lives with the dll in the same folder.
Alas, when you use your dll in another project, that project usually has it's own config, which takes precedence over the dll's one.

You could either add the dll's config to the parent's config, or try configuring what you need in code, instead of in the config.

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • you're reading it "manually" but it's not there. As I've said, and Thorsten as well, you'll have to either put them in the parent's config, or store them somewhere else . – Noctis Sep 02 '14 at 08:07
  • 1
    To put it a bit differently: Only an application will read configuration files - and it will only read its own configuration file. – Thorsten Dittmar Sep 02 '14 at 08:11
  • 1
    Ok, I understand I will do so – sabotero Sep 02 '14 at 08:14
2

In .NET DLLs can not have configuration files. They will simply not be used. They are created if you use the settings tab in the project properties, but they will not be read. What you need to do is merge the settings from that config file into the application's configuration (in your case, the web.config file).

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139