简体   繁体   中英

.Net shared class library configuration

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM