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. So How to get ConfigurationManager in Class Library So I can get credential of Client and make Payment
Generally a class in the library shouldn't be reading web.config directly and take settings in constructor.
However it is acceptable for library in some scenarios to read the web.config directly. For that you can define a custom configuration section or use 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.
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.
1) Add a reference to System.Configuration.
2) Use ConfigurationManager instead of WebConfigurationManager; the code will be almost the same, just replace WebConfigurationManager with ConfigurationManager.
I had this same issue, I am assuming you would like to use the ConfigurationManager
to access some properties in your config file.
An alternative is to add a settings file to the project. That is what I have done in my project:
- Right click on the project name, go to the Properties link on the menu.
- Click on the settings link on the left tab
- If you have not already generate one VS will give you a link to create one.
- Once you have created one, you can add key value pairs for the properties you require.
- 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#)
- You can then access these settings in your project's classes by doing
[ProjectNamespace].Properties.Settigns.Default.[PropertyName]
move your class library configuration settings to web.config.
© 2022 - 2024 — McMap. All rights reserved.