[英]How to use ConfigurationManager Class in a Class Library Project type
I have one class Library Project for Payment. 我有一个类付款库项目。 The live credential of client is on web.config file of website and this class library project will added in bin in form of dll. 客户端的实时凭证位于网站的web.config文件中,此类库项目将以dll的形式添加到bin中。 So How to get ConfigurationManager in Class Library So I can get credential of Client and make Payment 那么如何在类库中获取ConfigurationManager所以我可以获得客户端的凭证并进行付款
Generally a class in the library shouldn't be reading web.config directly and take settings in constructor. 通常,库中的类不应该直接读取web.config并在构造函数中进行设置。
However it is acceptable for library in some scenarios to read the web.config directly. 但是,在某些情况下,库可以直接读取web.config。 For that you can define a custom configuration section or use WebConfigurationManager.AppSettings 为此,您可以定义自定义配置部分或使用WebConfigurationManager.AppSettings
You need to add reference to System.Web assembly and you need to include the namespace System.Web.Configuration in the file where you want to use configuration manager. 您需要添加对System.Web程序集的引用,并且需要在要使用配置管理器的文件中包含命名空间System.Web.Configuration。
It's better if you can move that class away from the class library but if you can't or you don't want to do it, then you can work with System.Configuration. 如果你可以将这个类从类库移开,但如果你不能或者你不想这样做,那么你可以使用System.Configuration。
1) Add a reference to System.Configuration. 1)添加对System.Configuration的引用。
2) Use ConfigurationManager instead of WebConfigurationManager; 2)使用ConfigurationManager而不是WebConfigurationManager; the code will be almost the same, just replace WebConfigurationManager with ConfigurationManager. 代码几乎相同,只需用ConfigurationManager替换WebConfigurationManager即可。
I had this same issue, I am assuming you would like to use the ConfigurationManager
to access some properties in your config file. 我有同样的问题,我假设你想使用ConfigurationManager
访问配置文件中的一些属性。
An alternative is to add a settings file to the project. 另一种方法是将设置文件添加到项目中。 That is what I have done in my project: 这就是我在项目中所做的:
[ProjectNamespace].Properties.Settigns.Default.[PropertyName]
然后,您可以通过[ProjectNamespace].Properties.Settigns.Default.[PropertyName]
访问项目类中的这些设置[ProjectNamespace].Properties.Settigns.Default.[PropertyName]
将Reference System.Web
添加到类库中。
将您的类库配置设置移动到web.config。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.