类库可以有 App.config 文件吗?
这是我目前的解决方案:
在 Tutomentor.Branding 项目中,我喜欢在 App.config 文件中保存品牌信息,例如名称、颜色等。
在 Tutomentor.Data 项目中,App.config 是在我添加实体 .edmx 模型文件时创建的。
这可能吗?有什么建议吗?
部署时,输出会将这些 App.config 文件合并为一个文件吗?
Here is what my solution is looking like at the moment:
In the Tutomentor.Branding project, I'd like to save branding information in the App.config file, like names, colors, etc.
In the Tutomentor.Data project, the App.config was created when I added an entity .edmx model file.
Is this possible? Any recommendations?
When deploying, will the output COMBINE these App.config files into a single one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,类库可以保存设置文件,但它们的值将在应用程序配置(web.config、app.config...)中定义。
这是因为配置设置覆盖了功能。
您需要在应用程序的 app.config 或 web.config 中声明程序集的配置部分(WPF、SL、ASP.NET...),并为正确程序集中定义的特定数量的设置定义一个值设置。
编辑:
将设置文件添加到您的项目并添加具有应用程序范围的设置,您的程序集将具有如下内容:
现在您需要转到您的应用程序,并且需要复制粘贴节组和节声明,以及设置值的定义。就这样。
No, class libraries can hold setting files, but their values will be defined in the application configuration (web.config, app.config...).
That's because of configuration settings overriding feature.
You'll need to declare the assemblies' configuration sections in the app.config or web.config of your application (WPF, SL, ASP.NET...) and define a value for a particular number of settings defined in the proper assembly settings.
EDIT:
Add a setting file to your project and add a setting with application scope, and your assembly would have something like this:
Now you'd need to go to your application, and you need to copy-paste the section group, and section declarations, and the definition of the values for the settings. That's all.
虽然这是一个较旧的线程,但它确实值得重新审视。
看来您可能想以不同的方式看待这个问题。
类库本质上应该是可移植的。因此,任何需要的配置都应该传递给类,而不是驻留在库中。连接字符串之类的东西本质上是暂时的,因此将它们放在所属应用程序中是有意义的。
使用库中包含的方法时,您可以将任何所需的信息作为方法签名的一部分或作为类中的公共属性传递。我建议您为配置项创建公共属性,并在实例化类时传递它们。
现在您对 DLL 的 app.config 没有任何问题,并且 DLL 是真正可移植的。
While this is an older thread, It does warrent another look.
It seems you may want to look at the issue in a different way.
Class libraries by nature are supposed to be portable. So, any configuration needed should be passed to the class, instead of residing with the library. Things like connection strings are by nature transitory, so it makes sense to put them in the owning application.
When utilizing the methods contained in the library, you pass any needed information as part of the method's signature, or as a public property in the class. I suggest you create public properties for your configuration items, and pass them when you instantiate the class.
Now you have no issues with an app.config for the DLL, and the DLL is then truly portable.
只需创建您自己的 XML 文件,将其命名为 appConfig.xml 或类似名称,让您的类库使用 System.Xml 而不是 System.Configuration 读取该文件,并将该文件与您的 dll 一起打包。
Just create your own XML file, name it appConfig.xml or something similar, have your class library read the file using System.Xml instead of System.Configuration, and package the file along with your dll.
库 app.config 中的任何特定配置,您必须手动放入 exe 配置文件中。
Any specific configuration from library app.config, you have to put in your exe configuration file manually.
您可以添加设置文件,它将自动生成 app.config 文件。
并且可以以表格形式添加指定数据类型的键值对。
然后通过调用Settings.Default来访问这些值。[KEY]
可以参考:https://www.technical-recipes.com/2018/creating-a-user-settings-class-library-for-your-c-project/
You can add Settings File which will automatically generate app.config file.
And can Add the key value pairs in tabular form with the datatype specified.
Then Access the values by calling Settings.Default.[KEY]
can reffer: https://www.technical-recipes.com/2018/creating-a-user-settings-class-library-for-your-c-project/
好吧,类库不能有自己的 app.config,但是如果您在 exe 文件的 app.config 中的类库中定义设置,则类库应该能够找到这些设置。
我有点不确定但我不认为它会自动组合它们我想你必须手动做!
Well the class library can't have its own app.config, but if you define your settings the from the class library in the app.config for the exe file the class library should be able to find those to..
im a bit unsure but i don't think it will combine them automatically i guess you have to do i manually!
只需使用这个通用配置来处理类库中服务的 d.injection;
appsettingjson 文件:
//注释掉
just use this common config to handle d.injection of services in class lib;
appsettingjson file:
//commentout