为什么这个类库dll没有从app.config获取信息

发布于 2024-09-04 09:13:40 字数 698 浏览 3 评论 0原文

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

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

在此之前,只需构建 app.config:

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

然后在如下代码中获取它:

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

但是当我编译它并将其放入 web 服务的 bin 文件夹中时,它不断为 < code>shareDirectory,可能是因为找不到app.config。那么我如何确保包含此内容,这样我就不必对我的目录位置进行硬编码?我注意到编译后我们基本上会得到 assembly.dll 和 assembly.dll.config (即 app.config 文件),因此它肯定位于 bin 文件夹中!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

书信已泛黄 2024-09-11 09:13:40

这是因为您的 web 服务使用 web.config

That is because your web service uses web.config

是伱的 2024-09-11 09:13:40

您可能对类库的范围感到困惑。

请记住,您的配置(无论是 web.config 还是 app.config)是托管应用程序中存在的配置。在本例中,您的托管应用程序是 WebService,当然由 IIS 托管,因此您的配置文件是 web.config。

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

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.

千と千尋 2024-09-11 09:13:40

您需要将配置放在 web.config 文件中,而不是放在 assembly.dll.config 中:.NET 不会(根据设计)读取 assembly.dll.config 文件。

You need to put the configuration in your web.config file, not in assembly.dll.config: .NET does not (by design) read assembly.dll.config files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文