ConfigurationManager.AppSettings 变得为空?

发布于 2024-10-04 15:13:29 字数 539 浏览 3 评论 0原文

我没有意识到:“在单独的类库中有一个 web.config,并且”正在从不同的 Web 应用程序读取 web.config 应用程序设置。

我正在使用 VS2010 目标框架 3.5

我不知道这里出了什么问题,但当我尝试获取 ConfigurationManager.AppSettings["StoreId"]; 时,我得到 null

private string _storeid = GetStoreId;

public static string GetStoreId
{
    get
    {
        return ConfigurationManager.AppSettings["StoreId"];
    }
}

<强>web.config:

<appSettings>
    <add key="StoreId" value="123" />
</appSettings>

I did not realize that: 'have a web.config in a separate class library and' was reading the web.config app setting from different web application.

I am using VS2010 target framework 3.5

I don't know what is wrong here but I am getting null when I try to get ConfigurationManager.AppSettings["StoreId"];

private string _storeid = GetStoreId;

public static string GetStoreId
{
    get
    {
        return ConfigurationManager.AppSettings["StoreId"];
    }
}

web.config:

<appSettings>
    <add key="StoreId" value="123" />
</appSettings>

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

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

发布评论

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

评论(13

季末如歌 2024-10-11 15:13:29

如果您进行单元测试,则需要单元测试项目APP.CONFIG副本

If you are UNIT TESTING you need A COPY of the APP.CONFIG inside the UNIT TEST PROJECT

红玫瑰 2024-10-11 15:13:29

更新

您的 CsProj 文件中可以有一个 AfterTargets 来复制配置:

<Target Name="CopyAppConfig" AfterTargets="Build" DependsOnTargets="Build">
    <CreateItem Include="$(OutputPath)$(AssemblyName).dll.config">
         <Output TaskParameter="Include" ItemName="FilesToCopy"/>
    </CreateItem>
    <Copy SourceFiles="@(FilesToCopy)" DestinationFiles="$(OutputPath)testhost.dll.config" />
</Target>

问题

通常的原因是上下文造成的。

原因

当您有一个包含两个项目的解决方案时,如果 App/ Web.Config 位于主项目中,如果正在运行的应用程序的上下文是第二个项目(例如库、单元测试等),则它将无法工作。

难题

要从其他项目中的配置读取值(使用 System.Configuration)您需要将配置文件移动/复制到具有运行上下文的项目中。不幸的是,复制文件破坏了良好编程的基础; OOP、源代码管理、SOLID 等。

酷解决方案

一个漂亮的解决方案是在其他项目中添加配置文件快捷方式,这样您只需更新一个文件:

在此处输入图像描述

这会最好将 config 文件的内容划分到项目中。优雅地,就像按照答案 #2 共享程序集文件一样: https://stackoverflow.com/a/15319582/495455 但是唉,这是由上下文

Update

You can have an AfterTargets in your CsProj file that copies the config:

<Target Name="CopyAppConfig" AfterTargets="Build" DependsOnTargets="Build">
    <CreateItem Include="$(OutputPath)$(AssemblyName).dll.config">
         <Output TaskParameter="Include" ItemName="FilesToCopy"/>
    </CreateItem>
    <Copy SourceFiles="@(FilesToCopy)" DestinationFiles="$(OutputPath)testhost.dll.config" />
</Target>

Problem

The usual cause for this is due to context.

Cause

When you have a solution with two projects, if the App/Web.Config is in the main project it wont work if the context of the running application is the second project such as a library, unit test, etc.

Conundrum

To read values from the config in other projects (with System.Configuration) you'll need to move/copy the config file to the project with the running context. Unfortunately duplicating files defeats the tenants of good programming; OOP, Source Code Management, SOLID, etc.

Cool Solution

A nifty solution is adding config file shortcuts in other projects so you only update one file:

enter image description here

It would be nice to divide the contents of config files across project's. Elegantly, like Sharing Assembly Files as per answer #2: https://stackoverflow.com/a/15319582/495455 but alas it's by context

_畞蕅 2024-10-11 15:13:29

免责声明;)这篇文章不是为了回答OP,因为为时已晚,但它绝对会对最终访问此页面的读者有所帮助。

我遇到的问题< /strong> : ConfigurationManager.AppSettings["uName"] 在我的 C# Web api 项目中返回 null

我检查的基本内容

1) 在代码 ConfigurationManager.AppSettings["uName"] 中,我使用了精确的键 'uName',就像我在 web.config 文件中一样,

<appSettings>
      <add key="uName" value="myValue" />
</appSettings>

检查我没有错误输入作为 userName< /code> 而不是 uName 等。

2) 由于它是一个 Web API 项目,因此它的文件将是 web.config 而不是 < code>app.config ,以及项目的根文件夹中。 [参考图片]。

输入图像描述这里

解决方案

对我有用的解决方案,

更改 ConfigurationManager.AppSettings["uName"]WebConfigurationManager.AppSettings["uName"]

并且

确保我

<appSettings>
          <add key="uName" value="myValue" />
</appSettings>

正确文件中,即。

正确的文件不是视图文件夹中的 web.config

调试或发布 web.config

在此处输入图像描述

Disclaimer ;) This post is not to answer OP as it is too late but definitely it would help the readers who end up to this page.

Problem I faced : ConfigurationManager.AppSettings["uName"] returning null in my C# web api project.

Basic Things I checked for :

1) In code ConfigurationManager.AppSettings["uName"] , I was using exact key 'uName' as I had in web.config file,

i.e

<appSettings>
      <add key="uName" value="myValue" />
</appSettings>

Checked that I haven't mis typed as userName instead of uName etc.

2) Since it is a Web API project it would have a file as web.config instead of app.config , and that too in root folder of your project. [refer the image].

enter image description here

Solution :

The solution that worked for me ,

Changed ConfigurationManager.AppSettings["uName"] to WebConfigurationManager.AppSettings["uName"]

and

made sure that I had

<appSettings>
          <add key="uName" value="myValue" />
</appSettings>

in the right file ie.

Right file is not web.config in View folder

neither the debug or release web.config

enter image description here

坏尐絯 2024-10-11 15:13:29

and:

<appSettings>
    <add key="StoreId" value="123" />
</appSettings>

位于 ASP.NET 应用程序的 web.config 文件中,而不是在 Visual Studio 中添加到类库项目中的某些 app.config 文件中, 正确的?如果是这种情况,您不可能得到 null。如果您已将其添加到 Visual Studio 中类库项目的 app.config 中,那么获取 null 是完全正常的行为。

and:

<appSettings>
    <add key="StoreId" value="123" />
</appSettings>

is located in the web.config file of your ASP.NET application and not in some app.config file you've added to your class library project in Visual Studio, right? You can't be possibly getting null if this is the case. If you've added this to an app.config of you class library project in Visual Studio then getting null is perfectly normal behavior.

怀念你的温柔 2024-10-11 15:13:29

我刚刚得到答案 DLL 是从另一个项目调用的,而不是在 App.config 中有 create.so 条目的项目中调用,应该移动到调用项目配置文件。

例如,我的解决方案中有 2 个项目,一个类库和其他控制台应用程序。我已在控制台应用程序中添加了类库引用。因此,如果我在类库项目中添加 app.config 文件,它会通过 null 异常。当我添加应用程序时它会起作用控制台应用程序中的.config。希望它有效

I just got answer DLL are called from another project not in the project where there are create.so entries in App.config should b move to calling project config file.

For example i have 2 project in my solution one class library and other console application.i have added class library reference in Console application.So if i add app.config file in class library project it through null exception.it works when i added app.config in console application.Hope it works

噩梦成真你也成魔 2024-10-11 15:13:29

应用程序设置会加载到 ConfigurationManager.AppSettings 中,但用户设置(在项目属性的属性设置中)不会加载。

App settings are loaded into ConfigurationManager.AppSettings, but User settings (in Properties settings in your project properties) are not.

不气馁 2024-10-11 15:13:29

在 Visual Studio 中,右键单击配置文件,选择“属性”,然后将“复制到输出目录”更改为“始终复制”或“如果较新则复制”。

或者,手动添加以下部分作为 .csproj 文件中元素的子元素(此部分用于文件“App.config”的“始终复制”):

  <ItemGroup>
    <None Update="App.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

In Visual Studio, right-click on the config file, select Properties, and then change "Copy to Output Directory" to either "Copy always" or "Copy if newer".

Alternatively, manually add the following section as a child of the element in your .csproj file (this one is for "Copy always" for file "App.config"):

  <ItemGroup>
    <None Update="App.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
鱼窥荷 2024-10-11 15:13:29

我尝试了所有这些解决方案,但没有一个对我有用。我试图使用“web.config”文件。所有内容都被正确命名并且文件位于正确的位置,但它拒绝工作。然后我决定将我的“web.config”文件重命名为“app.config”,就像这样,它起作用了。

因此,如果您遇到“web.config”文件的问题,请务必将其重命名为“app.config”。

I tried all of these solutions but none worked for me. I was attempting to use a 'web.config' file. Everything was named correctly and the files were in the proper location, but it refused to work. I then decided to rename my 'web.config' file to 'app.config' and just like that, it worked.

So if you are having this issue with a 'web.config' file be sure to rename it to 'app.config'.

层林尽染 2024-10-11 15:13:29

当我测试类库(.dll)时,这发生在我身上。它们都在同一个项目中,但库的 App.config 具有我需要的设置。我编写的测试应用程序需要这些设置,因为正在运行该库。

This happened to me when I was testing a Class Library (.dll). They were both in the same project but the App.config for the library had the settings I needed. The App I had written to test needed the settings because it was running the library.

你如我软肋 2024-10-11 15:13:29

当我从文件资源管理器复制项目并重命名该项目时,我遇到了这个问题。这复制了 Debug 文件夹,并且由于我没有将其设置为“如果较新则复制”,因此它不会覆盖旧的 App.config 文件。

只需删除 Debug 文件夹并重建即可。希望对某人有帮助。

I got this problem as I copied a project from the file explorer and renamed the project. This copied the Debug folder and as I didn't have it set to 'Copy if newer' it didn't overwrite the old App.config file.

Just delete the Debug folder and rebuild. Hope that helps someone.

感情洁癖 2024-10-11 15:13:29

我同意上面的答案,我想补充几点

  1. 你应该确保你没有在之前和之后添加空格:
    参见下面的代码:

    私有静态字符串 Client_ID = ConfigurationManager.AppSettings["ida:ClientId"];
    

    如果您在 ida: ClientId 之间放置空格,它将不起作用并返回 null

  2. 确保您的键值名称正确

  3. 您可以尝试 WebConfigurationManager

I agree with above answer and I would like to add few more points

  1. you should make sure you don't put space before and after the :
    see code below:

    private static string Client_ID = ConfigurationManager.AppSettings["ida:ClientId"];
    

    if you put space between ida: ClientId it will not work and will return null

  2. make sure your key value names are correct

  3. you can try WebConfigurationManager

jJeQQOZ5 2024-10-11 15:13:29

刚才发生在我身上,只是从另一个项目调用它时。
显然,在另一个项目中,引用并未定义为服务引用,而是定义为连接服务。我删除了引用并重新添加了它。

Happened to me just now, only when calling it from another project.
Apparently, at the other project, the reference has not been defined as a Service Reference but rather as a Connected Service. I deleted the reference and added it again.

夏日浅笑〃 2024-10-11 15:13:29
string setting = ConfigurationManager.AppSettings["Setting"];
  1. 确保配置文件与引用它的代码位于同一文件夹中。创建一个
    辅助类(如果不是)。重复此操作可能会导致混乱
    有人忘记它存在于两个地方。
  2. 将应用程序设置保留在 app.config 中,而不是 AppSettings 的 web.config 中。我在 web.config 中的密钥上遇到了这个问题。
string setting = ConfigurationManager.AppSettings["Setting"];
  1. Make sure config file is in the same folder as code referencing it. Create a
    helper class if it is not. Duplicating this could cause confusion should
    someone forget it exists in two places.
  2. Keep app settings in an app.config and not a web.config for AppSettings.I had this issue with a key in the web.config.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文