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

Steve
  • 25,806
  • 2
  • 33
  • 43
Akil Vhora
  • 319
  • 1
  • 5
  • 21

5 Answers5

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.

Muhammad Hasan Khan
  • 34,648
  • 16
  • 88
  • 131
  • Please Can you write some code for that because I got code from your link but still WebconfigurationManager not shown in intelligence of studio – Akil Vhora Nov 05 '11 at 09:27
  • @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. – Howiecamp Jan 24 '17 at 15:32
  • 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. – Muhammad Hasan Khan Jan 24 '17 at 21:55
2

Add Reference System.Web to your Class Library.

Alien
  • 15,141
  • 6
  • 37
  • 57
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.

Francisco Goldenstein
  • 13,299
  • 7
  • 58
  • 74
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]
strizzwald
  • 643
  • 1
  • 8
  • 19
  • 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. – eaglei22 Jan 17 '19 at 14:19
0

move your class library configuration settings to web.config.

Sheikh Ali
  • 1,514
  • 1
  • 10
  • 8