.Net shared class library configuration - c#

I have a class library which needs some configuration to function. This class library is referenced by multiple applications (Multiple ASP.Net websites, and Windows Forms applications)
It is my understanding that it is possible to store the configuration in the library's app.config => myDll.dll.config file. See: Putting configuration information in a DLL, and C# Dll config file
My issue is that I don't want to manually handle copying the config file to the bin folder of every host assembly. Is there a mechanism in .Net to handle pairing of the dll to its config file so that the accompanying configuration is copiled along with the dll whereever it is distributed/referenced?

If the config is the same for all instances of your dll, then I'd add it as an embedded resource, so it's part of your dll and not a separate file at all.
TO do this, either add it as a file resource to your Resources.resx file, or just add the file directly to your Project and then set its compile type (in the Properties window) to Embedded Resource.
You can then use Assembly.GetExecutingAssembly.GetManifestResourceNames() to list the names of the resources in your dll, and Assembly.GetExecutingAssembly.GetManifestResourceStream() to get a stream to read the file's data from. I'd probably use a simple homebrew XML format for my data and then an XmlTextReader/XmlDocument to read it very easily back in.

You'l have to deploy this .dll into GAC and put there the config file, all apps will first search the GAC when loanding a reference. Here is how you can deploy the dll + config.

Related

How to Include an App.Config file of a Class Library in the Web Publish release package

I have ran into a scenario where I have added an App.Config file in a Class Library which I am referencing from my ASP.NET MVC project.
Now while publishing my Web project I can only see the DLL of the class library in the Bin folder but not the App.Config of the same. Is there any way to include that in the Web publish.
Thanks
You need to tell the compiler to copy these files to the output directory. To do so, select the app.config from the solution explorer and hit F4 for properties and select Copy Always like the below example
Your class library shouldnt be using the app.config file. Instead the driver application or consuming application should be having a single app.config and should feed this dll with parameters.
This will also help your application being more SOLID. Your class library should be doing single job. Reading from a app.config file adds a second responsibility.
Also, class library not reading from the app.config file make your class library loosely coupled and it can be taken anywhere.

Cannot find .config file for a dynamically loaded 3rd party assembly used by ASP.NET Service

C#, VS2010, .Net 4.0, ASP.NET service under IIS or IIS Express.
I'm using a 3rd party assembly within my service. This 3rd party assembly has a .config file and is loaded dynamically as such:
Service -> MyAssembly1 -> Dynamically Load MyAssembly2 using Assembly.Load() -> 3rd Party Assembly with .config
I'm normally a C++ developer so this ASP.NET stuff is rather new to me.
I understand that with an ASP.NET application the assemblies it uses are "shadow-copied" to the Temporary ASP.NET Files folder.
Unfortunately when this 3rd party assembly is "shadow-copied" its .config file is not. So when it comes time to use this assembly it's failing because it cannot open its .config file.
From within my assembly I'm able to get the CodeBase for that assembly and load the .config file myself, but that doesn't help me since this 3rd party assembly needs to open the file itself.
If I manually copy the .config file to the shadow-copy location it works great.
Also, this 3rd party called the config file "assemblyname.config" instead of "assemblyname.dll.config".
The error I get is:
Unable to load configuration file: "\assemblyname.config".
When this 3rd party assembly is used in a non ASP.NET app (ie a normal Windows app) it works great since it doesn't do the shadow-copy stuff. Just uses the assembly and .config file directly from the exe location.
Any ideas? I've been unable to find a solution though I'd think this would be a rather common issue.
The main thing when loading assemblies is to see the path where the assembly is placed on disk and the page directory from which you are trying to load the assembly. So if you are adding the path for loading the assembly that does not mean that from the same path it will load the config file. Please try to put them both in the root folder of the app and see if the issue remains.

How Enterprise Library loads hierarchy of external configuration files

Please, can someone clarify to me a loading process? Say, you have app.config file for your UI executable. You add Enterprise Library config sources in that file. Each config source refers to another config file, say, one for logging dll, one for data storage dll and one for services dll. In each aux configs you write sections related to Enterprise Library application blocks and some core sections from .NET System.Configuration namespace (for example, connectionStrings).
Now, when configuration system initialized during UI startup, how this hierarchial configuration will be loaded?
As I understand, Enterprise Library will load it's own configuration sections, and will follow file name links and load external configuration files for each FileConfigurationSource. And after will load it's own configuration sections from each external file, mixing them all together.
What about core .NET sections? Will it be loaded also hierarchially by means of Enterprise Library, or I should use configSource attribute for this purpose?

What happens to the app.config file once I reference the class library

I have a class library with a app.config file with 4 settings in it. Previously when I tried this (VS2008) as soon as I referenced the library I would not be able to access the config file/application would break, but for some reason it works now. I have referenced the library in 3 applications and I can still access the settings fine but the actual config file is missing.
Does the config file get compiled along with the library as soon as you reference it or what happens to it?
(working in vs2010)
It probably works because there are defaults in the generated Settings.Designer.cs file. The more important question would be: How can I change my settings?
And that would require copying or merging the relevant XML sections from the library app.config to that of the referencing application.
The config file is renamed; "app" is replaced by the name of your assembly. So if your app.config is associated with a program, say Foo.exe, then in your output folder (eg bin\debug) you should have a Foo.exe.config. If your app.config is associated with a class library, say Bar.dll, then in the output folder it will be Foo.dll.config.

Config files for GAC objects

Typically we keep our config values in web.config/app.config and for the environment global config varialbes (and not application specific) in server machine.config file.
When deploying an object to the GAC where is the best location to keep these config type values? It seems that the best location would be a linked resource file.
Does anyone have any experience/recommendation with this approach? (sample code?)
thx
The configuration values need to be in the application configuration of the executing assembly. It is the responsibility of the application to have the configuration values so that your assembly will have access to them when it is loaded into the AppDomain.
I've had a need for assembly-specific config files (as opposed to executing assembly config files) in the past.
For an assembly in the GAC, it is possible (but not recommended) to physically copy a config file to the assembly dll folder.
The solution I've used for creating a config file that can be shared across assemblies regardless of AppDomain is a a simple registry entry for my application that defines a shared config file location. This way, any assembly can retrieve configuration settings from a shared location, regardless of which executing assembly launched it. (especially useful for scripting - otherwise, you'd have to deploy a config file named wscript.exe.config in the windows\system32 folder - UGH!)
if you dont care of having specific configuration for each application using your dll you can place the configuration in the machine.config file inside the framework folder.
%systemRoot%/Windows/Microsoft.Net/Framework/[Version]/Machine.config

Categories