为什么这个类库没有从app.config获取信息 [英] Why is this class library dll not getting information from app.config

查看:241
本文介绍了为什么这个类库没有从app.config获取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个自定义的HttpHandler,为此我写了一个C#类库并编译到DLL。



作为其中的一部分,我有一些目录位置在应用程序中不想硬编码,所以我试图把它放在我以前使用过的app.config中。



在这之前,构建 app.config:

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

然后在代码中获取此代码:

  var shareDirectory = ConfigurationManager.AppSettings [Share]; 

但是当我编译它并将其放在webservice的bin文件夹中时,它会保持为null shareDirectory ,可能是因为找不到app.config。那么如何确保这是包含在内,所以我不必硬编码我的目录位置?我注意到它将基本上在编译后得到assembly.dll和assembly.dll.config这是app.config文件,所以它的确定在bin文件夹中!

解决方案

你可能会混淆你的类库的范围。



记住,你的配置,无论是web.config,或app.config,是HOSTING应用程序中存在的配置。在这种情况下,您的托管应用程序是WebService,由IIS托管,所以您的配置文件是web.config。



如果您有一个控制台应用程序,以某种方式使用该类库(尽管可能不在http处理程序上下文中),那么配置将是控制台应用程序中的app.config,而不是您的类库中的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!

解决方案

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.

这篇关于为什么这个类库没有从app.config获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆