简体   繁体   English

.Net共享类库配置

[英].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) 多个应用程序(多个ASP.Net网站和Windows Forms应用程序)引用了该类库。

It is my understanding that it is possible to store the configuration in the library's app.config => myDll.dll.config file. 据我了解,可以将配置存储在库的app.config => myDll.dll.config文件中。 See: Putting configuration information in a DLL , and C# Dll config file 请参阅: 将配置信息放入DLLC#Dll配置文件

My issue is that I don't want to manually handle copying the config file to the bin folder of every host assembly. 我的问题是,我不想手动处理将配置文件复制到每个主机程序集的bin文件夹中的情况。 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? .Net中是否存在一种机制,可将dll与其配置文件配对,以便随随便便在分发/引用该dll的情况下编译随附的配置?

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. 如果dll的所有实例的配置都相同,那么我会将其添加为嵌入式资源,因此它是dll的一部分,而不是一个单独的文件。

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. 为此,可以将其作为文件资源添加到Resources.resx文件中,或者直接将文件添加到Project中,然后将其编译类型(在“属性”窗口中)设置为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. 然后,您可以使用Assembly.GetExecutingAssembly.GetManifestResourceNames()列出资源的名称在您的DLL,并Assembly.GetExecutingAssembly.GetManifestResourceStream()得到一个流读取文件的数据。 I'd probably use a simple homebrew XML format for my data and then an XmlTextReader/XmlDocument to read it very easily back in. 我可能会为数据使用简单的自制XML格式,然后使用XmlTextReader / XmlDocument来很容易地读回。

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. 您必须将此.dll部署到GAC中,并放置配置文件,所有应用程序在借用参考时都会首先搜索GAC。 Here is how you can deploy the dll + config. 是部署dll +配置的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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