简体   繁体   English

如何在类库项目类型中使用ConfigurationManager类

[英]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: 这就是我在项目中所做的:

  1. Right click on the project name, go to the Properties link on the menu. 右键单击项目名称,转到菜单上的“属性”链接。
  2. Click on the settings link on the left tab 单击左侧选项卡上的设置链接
  3. If you have not already generate one VS will give you a link to create one. 如果您尚未生成一个VS,则会为您提供创建一个的链接。
  4. Once you have created one, you can add key value pairs for the properties you require. 创建一个后,可以为所需的属性添加键值对。
  5. VS will include a settings.settings file in your project which will contain the properties that you have added in the settings file (you should see them under Settings.Designer.cs assuming you are using C#) VS将在项目中包含一个settings.settings文件,该文件将包含您在设置文件中添加的属性(假设您使用的是C#,您应该在Settings.Designer.cs下看到它们)
  6. You can then access these settings in your project's classes by doing [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.

相关问题 在类库中找不到ConfigurationManager - Cannot find ConfigurationManager in class library ConfigurationManager类别 - ConfigurationManager Class 如何在类库项目中使用TagBuilder? - How to use TagBuilder in a Class Library project? 如何在类库项目中使用Autofac? - How to use Autofac in a class library project? 在单元测试中使用时,是什么导致 ConfigurationManager 不适用于 class 库项目? - What is causing ConfigurationManager not to be working for class library project when used in unit test? 如何将项目类型转换为共享类库类型 - How do I convert a project type to shared class library type 如何通过类库项目中的类获取主项目中类的类型 - How to get the Type of a class in Main Project through a class in Class Library Project 如何在Visual Studio中使用类库的输出类型调试项目? - How to debug a project with Output Type of Class Library in Visual Studio? 在类库的 App.config 中使用 ConfigurationManager.GetSection - Using ConfigurationManager.GetSection in a Class Library's App.config 在类库中无法访问ConfigurationManager.AppSettings [key] - ConfigurationManager.AppSettings[key] not accessible within class library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM