Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this class library dll not getting information from app.config

I am developing a custom HttpHandler, to do so I write a C# Class Library and compile to DLL.

As part of this I have some directory locations which I want not hard coded in the app, so i'm trying to put it in the app.config which I've used before.

Before this has worked by just going building the app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Share" value="C:\...\"/>
  </appSettings>
</configuration>

And then obtaining this in code like:

var shareDirectory = ConfigurationManager.AppSettings["Share"];

But when I compile it and put it in the bin folder for the webservice, it keeps getting null for shareDirectory, probably because it can't find app.config. So how do I make sure this is included so I don't have to hard code my direcotry locations? I notice it will essentially after compiled we get the assembly.dll and the assembly.dll.config which is the app.config file, so it's definetly there in the bin folder!

like image 340
baron Avatar asked Dec 04 '25 17:12

baron


2 Answers

That is because your web service uses web.config

like image 138
Aren Avatar answered Dec 06 '25 10:12

Aren


You're probably confusing the scope of your class library.

Remember, your config, be it web.config, or app.config, is the config present in the HOSTING application. In this case your hosting application is the WebService, hosted of course by IIS, so your config file is the web.config.

If you had a console app which somehow used that class library (though probably not in an http handler context), then the config would be the app.config in the console app, not the app.config in your class library.

like image 20
andy Avatar answered Dec 06 '25 09:12

andy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!