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!
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.
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).
Related
Publishing a .net core application (like WPF) as a single .exe file is nice, but what if you had some config files in your applicaton? How is that supposed to work?
For example, what do I do if I have a connection string in a config file and I want to change it without compiling?
Config files are still present in your publish folder, and you can change the value as before.
The difference you will see is that instead of having a bunch of dll files in your publish folder you have only an executable and some configuration files like appsettings.json and web.config.
You can take a look at Microsoft's documentation and this dotnetcoretutorials post
I have integrated a dll with a service(exe). The dll has its own app.config file. But the code inside the dll for any Key/Value reference, it always refers to the exe's config file instead of the dll's config file. How the make the dll's config file as current to be used only when referred from inside the dll's code?
A DLL (or library file) cannot have its own config file. It will ALWAYS use the "hosting" applications configuration files.
The whole point of a DLL is that is can be used by many different applications and does not have configuration itself. Instead its "host" will contain the configuration information, so this is "by design".
When your solution contains more than one project, you can add app.config in any of the project; but when you compile the code to generate final output, you get only one configuration file named <exeFileName>.exe.config . This is the only configuration file that will be read by the WinForms framework when the application is running.
If your DLLs (Assemblies in .NET) specify their own configuration file, those config files will not become a part of the final output (the respective assemblies).
To include the settings from the project specific config files, you will have to make those configuration entries (appSettings, connectionString, etc) into the main application's configuration file.
The DLLs can also read from the configuration file of the main application. Hence, when you make those entries in the <exeFileName>.exe.config file, all the DLLs, that are loaded can read values from the file.
Note: Take care of using unique names for the keys for each DLL as the configuration values are read using the keys.
I created a console application to import "C" Dll using DllImport
The "C" Dll has reference to other Dll's and Config files in same folder.
When i put all the files in bin directory of Console applicaiton it works perfect.
But when i create a Web application to consume the same it does not work. (The dll reference are fine when i set the path, but it seems the config files are not getting loaded)
I have tried below but did not help.
Copy the DLL's and config files to System32.
Give access permission to IIS.
Disable shadow copy.
None seem to work, Any other suggestions?
I solved the issue.
I used Directory.SetCurrentDirectory(); to set the path of the dll's and config files.
if its not possible to change the current directory. Place the files in path Directory.GetCurrentDirectory();
You'll need to move the configuration into the web.config file for the web application
There's some good advice on how to do that here: Custom section/collection in Web.Config
I have several C# console applications, which need to have the same set of settings. I want to avoid duplicity and avoid separate app.config for each application.
Is there any way to read a common app.config file (say common.config) for applications (app1.exe, app2.exe).
Create one file called app.config. Put it in some place outside of your projects' directories, like up in the solution directory. Add it to your projects as a linked item with a relative path to the file. Set the right build action for this item (application configuration) in each project.
Now when each project builds, the file will be copied to the project's output dir with the right name.
You can load an external app.config using the code below:
config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
string logpath = config.AppSettings.Settings["Log.Path"].Value;
And save settings as so:
config = ConfigurationManager.OpenExeConfiguration(Path.Combine("C:\test\root", "Master.exe"));
config.AppSettings.Settings["Log.Path"].Value = "C:\newpath";
config.Save();
You might have to have a master config within one of the applications and point the rest to this. Typically this method is considered bad practice though. There might be issues with different applications locking the file.
#Ran's answer is an option, but each application will still have its own config file after you build. At compile time they will be the same, but at deploy time they are copies.
You can also open one application's config file from another application using:
ConfigurationManager.OpenExeConfiguration(string)
You can have an external config file that all applications reference using:
ConfigurationManager.OpenMappedExeConfiguration
And there's the option to using the Machine config file using:
ConfigurationManager.OpenMachineConfiguration()
I'm a bit lost!
I have a dll that uses an xml config file for some db connection info. The dll looks for the xml config file in it's own directory and I can't change the dll at all.
Every time I build the project, I must manually copy the config file into a folder way down somewhere in the Temporary ASP.NET Files folder. (I don't understand this but I can live with this manual change)
The problem is that when I publish the project, I can't figure out where to copy the config file to?
Could someone please point me in the right direction? Or maybe show me a way that I can 'bind' the xml config file to the bin folder???
Vauneen
The .NET config file can be confusing to manage. The way it works in a webapp is that the Web.config will supersede any dependencies' app.config files (which is what I assume you're talking about when you say "DLL".)
Basically, in .NET all config info is pulled from the main app project.
See:
App.config seems to be ignored
and
Configuration from App.config isn't being pulled correctly
and finally:
Does a web.config substitute app.config?
will probably help you figure out what you need to know.
Update: Doing some further searching on your problem, it's possible that the code you're incorporating into the .Net Solution is using the "obsolete" ConfigurationSettings which will require you to add a reference to System.Configuration in your references folder (right-click on the project -> "Add References" and go to the .NET tab and select System.Configuration).
Set "Copy to Output Directory" to "Copy always" in properties of the configuration. Then Visual Studio will copy the configuration file automatically after each build and it will be properly published as well.