1

I have a project in my solution that is a class library. I have two console applications in the solution that reference this library. There is an App.config in the library that I have added by link to the console applications and everything is great.

I just added a web application to the solution and referenced the class library. The class library has multiple references along the lines of var foo = ConfigurationManager.AppSettings["Foo"];. This works fine in the console applications, but when running the web application I get an exception that "Value cannot be null. Parameter name: value" on the line for the ConfigurationManager access.

I've modified my web.config to match the below:

<configuration>
  <appSettings file="App.config">
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

I've added the app.config as an existing object to the web application project as a link. I've set it to always copy when building.

I've looked at this and that am still cannot get things to work.


For additional clarification. Web Application calls a method in a class library. The class library uses ConfigurationManager. This fails. In the same solution there are Console Applications that do the same thing and work.

gilliduck
  • 2,762
  • 2
  • 16
  • 32
  • @mason Post that as an answer – JamesFaix May 08 '18 at 19:20
  • @mason, I could use an example of how to not duplicate the same information in three different projects then. The shared library isn't actually using the app.config, it's technically being provided by the other projects. It just happens to physically be there but is being used by link from the others. – gilliduck May 08 '18 at 19:20
  • You have a web application and a console application. They might use the same configuration information, but they each need their own copy of it. – mason May 08 '18 at 19:21
  • @mason, what do you propose as a solution so that 3 different projects (2 console and 1 web) in the same solution all have the same config information to pass to the library, without having to have duplicate files (that run the risk of not being kept current with each other)? – gilliduck May 08 '18 at 19:22
  • Put the same configuration in each one. Problem solved. There's an infinite number of ways to do your configuration: store it in a configuration database, have configuration web service, environment variables, custom configuration files etc. Or have your deployment processes inject the correct configuration values. Just pick something that works well for you. – mason May 08 '18 at 19:23
  • I am also having same issue... – Ziggler Jul 03 '19 at 00:22

1 Answers1

1

ASP.NET doesn't use app.config. It use web.config. ConfigurationManager will read from the config file appropriate to the project type. You really shouldn't have a .config file in a class library, instead have the consuming applications store the configuration in the appropriate config file for that project type.

mason
  • 31,774
  • 10
  • 77
  • 121