Net dll read app config with app config

System Requirements: Windows 8, Windows 7, Windows 8.1


What you are asking has been asked many times again and again. in fact you could instruct. NET to read configuration settings from another file, in your case will be something like library.dll.config, there are projects or classes to do this called something like assemblysettingsreader or similar. My suggestion is anyway to do not do it. It sounds cool initially but think in this way, your library is not running alone, can be referenced by a console app, a web site or a wpf ui project, all these clients might need to customize the configuration and have different settings so it makes full sense to have the settings related to the calling application config files.
Introduction When developing DLLs for distribution, it is often desired to have an app.config file associated with that DLL. However, the. NET 1.1 Framework only allows a single app.config and it is usually associated with the running application. This really leaves developers in a pinch as they do not know if an application is using an app.config to merge its entries into, or they require the application developer to include entries into their config file for DLL usage. Background One method commonly used is to write your own XML file and put your configuration info there. This method has several drawbacks. What if you have included a third party DLL that has configuration information that it expects to see in an app.config file? You are now stuck with the same problem. One such case of this problem arises when you use the Microsoft Enterprise Library in your DLL. The Libraries configuration tool adds a line into the app.config file that points to its own config file for internal use. When you call a library class, it looks in the app.config file for the name of its own config file. Things get ugly from here. If you have attempted to Google an answer to this problem, you will find can't be done. This article will explain how it can be done. I want to start by stating the following disclaimer(s It is not a normal practice to use reflection to patch back another class. This example is an extreme case to get around what I consider is a serious flaw in the. NET Framework. Portability was not a factor when developing this technique. This technique is not needed with. NET 2.0 in my understanding. All that said, let's get started. Using the code The. NET Framework reads the application configuration file into a static hashtable whenever the application domain is first referenced. Once it is read, it cannot be reread or modified. This is unfortunate as it does not allow.
« One Off Overrides of ajax Start PSA: Don’t Trust Autocomplete » I write a lot of reusable components. Most recently, I created an assembly to handle notifications in various projects. For this assembly, I needed various configuration options, such as an SMTP server address. For configuration in. NET projects, we turn to App.config files, and in ASP. NET projects, we have the Web.config. In the case of a DLL however, it isn’t really advised to create/use an App.config file. On Stack Overflow, Chris Ammerman goes into great detail on the topic. But, hey, what if you want to use one anyway? Attempt 1 Reading an assembly’s configuration file is pretty easy, you can access the App Settings section by opening the configuration using the Assembly. Get Executing Assembly. Location like so: var dll Path = Assembly. Get Executing Assembly. Location; var dll Config = Configuration Manager. Open Exe Configuration(dll Path / Get the app Settings section var app Settings = ( App Settings Section) dll Config. Get Section( app Settings return app Settings. Settings; That’s as easy as it is; or is it? This method works fine if you’re using the DLL in a Windows or console application, but try using it in an ASP. NET/ ASP. NET MVC application, and you’ll find that Assembly. Get Executing Assembly. Location points to something like C:\ Windows\ Microsoft. NET\ Framework64\v 9\ Temporary ASP. NET Files\blah\blah\blah (the ASP. NET shadow copy directory). Navigate to that directory, and you won’t find your config file there. One solution could be to copy the file to the temp directory, but that isn’t going to work in the long term. There has to be a better solution! Solution After searching the web for a while, I came across others with my problem. No solution there (although, I have since added one). Continuing my search, I stumbled upon a question about how to use reflection to get the bin path instead of.
This post relates to wider. Net app.config files and linked DLL’s, but more specifically, my instance was relating to the Revit API. For those that don’t know, an app.config file is a nice, easy way of doing configuration for your applications in.net. You can use the System. Configuration namespace to quickly access an XML config file without needing to do any manual XML reading. Unfortunately, you can only have one app.config file per executable, so if you have DLL’s linked into your application, they cannot have their own app.config files. Seeing as the Revit API is done by external. Net DLL files which are opened within the Revit environment, this applies to them as well. At Bornhorst + Ward, we have all of our external commands on a central network share, and each staff members ini is edited (using a tool I’ve written) to point to these external commands. I’d previously tried to use app.config files, and found they weren’t working, it seems they weren’t looking for the correct file in that network directory. The way around it for me, was to manually locate the app.config file in my code. 1: /

2: / Get the configuration for the supplied type 3: / 4: / type of class 5: / a configuration 6: public static System. Configuration. Configuration Get Config( Type type) 7: 8: /workout app.config lokcation 9: string dll Location = type. Assembly. Location +.config ; 10:  11: if (dll Location = null) 12: throw new Exception( Could not find config file, add.config in DLL location 13:  14: /create config 15: Exe Configuration File Map file Map = new Exe Configuration File Map 16: file Map. Exe Config Filename = dll Location; 17: System. Configuration. Configuration config = Configuration Manager. Open Mapped Exe Configuration(file Map, Configuration User Level. None 18:  19: return config; 20: Seeing as app.config.
It is not trivial to create a. NET configuration file for a. DLL, and for good reason. The. NET configuration mechanism has a lot of features built into it to facilitate easy upgrading/updating of the app, and to protect installed apps from trampling each others configuration files. There is a big difference between how a DLL is used and how an application is used. You are unlikely to have multiple copies of an application installed on the same machine for the same user. But you may very well have 100 different apps or libraries all making use of some. NET DLL. Whereas there is rarely a need to track settings separately for different copies of an app within one user profile, it's very unlikely that you would want all of the different usages of a DLL to share configuration with each other. For this reason, when you retrieve a Configuration object using the normal method, the object you get back is tied to the configuration of the App Domain you are executing in, rather than the particular assembly. The App Domain is bound to the root assembly which loaded the assembly which your code is actually in. In most cases this will be the assembly of your main. EXE, which is what loaded up the. DLL. It is possible to spin up other app domains within an application, but you must explicitly provide information on what the root assembly of that app domain is. Because of all this, the procedure for creating a library-specific config file is not so convenient. It is the same process you would use for creating an arbitrary portable config file not tied to any particular assembly, but for which you want to make use of. NET's XML schema, config section and config element mechanisms, etc. This entails creating an Exe Configuration File Map object, loading in the data to identify where the config file will be stored, and then calling Configuration Manager. Open Mapped Exe Configuration to open.