How to use ConfigurationManager Class in a Class Library Project type
Asked Answered
A

5

13

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

Alameda answered 5/11, 2011 at 9:18 Comment(0)
U
2

Add Reference System.Web to your Class Library.

Unstep answered 20/8, 2018 at 4:54 Comment(0)
R
10

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.

Rhearheba answered 5/11, 2011 at 9:22 Comment(3)
Please Can you write some code for that because I got code from your link but still WebconfigurationManager not shown in intelligence of studioAlameda
@Muhammad - Can you elaborate on this comment? << Generally a class in the library shouldn't be reading web.config directly and take settings in constructor. >> Why is that? I assumed architecturally this was ok as the class library is loaded in the context of the web application host.Blacksmith
if you unit test your class, you won't have a web.config to feed it, also libraries are generally re-usable utilities that shouldn't be tied to which platform or context they run in, unless you have a specific library that is only for web based applications, in that case you can define your own config section, since config is owned by the application not the library.Rhearheba
U
2

Add Reference System.Web to your Class Library.

Unstep answered 20/8, 2018 at 4:54 Comment(0)
J
1

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.

Jecon answered 18/6, 2014 at 20:24 Comment(0)
P
1

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:

  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.
  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#)
  6. You can then access these settings in your project's classes by doing [ProjectNamespace].Properties.Settigns.Default.[PropertyName]
Posterity answered 20/9, 2016 at 10:20 Comment(1)
The problem with this is, what is the point of having a separate class library that is meant to be portable, if you're going to tightly couple it to one application.Research
G
0

move your class library configuration settings to web.config.

Goggleeyed answered 5/11, 2011 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.