ConfigurationManager无法读取配置文件。

huangapple go评论68阅读模式
英文:

ConfigurationManager not reading configuration file

问题

Edit: 我选择了错误的启动项目,因此使用了不同的 app.config 文件。

我今天换了一台新笔记本电脑,正在尝试重新设置我的开发环境。

我们的 WPF 客户端使用 ConfigurationManager 构建连接字符串以访问我们的数据库。app.config 文件包含一些占位符,但实际凭据存储在用户机密中。配置是这样访问的:

server = ConfigurationManager.AppSettings["dbserver"];
db = ConfigurationManager.AppSettings["db"];
user = ConfigurationManager.AppSettings["dbuser"];
pwd = ConfigurationManager.AppSettings["dbpassword"];

在旧笔记本上,这个工作正常。在新笔记本上,我得到了 null 值,甚至不是占位符的值。你有什么想法,我在旧笔记本上可能配置得不同吗?新笔记本是 Windows 11 而不是 Windows 10,可能会有所不同吗?

app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        [...]
        <section name="configBuilders"
                 type="System.Configuration.ConfigurationBuildersSection, 
                 System.Configuration, Version=4.0.0.0, Culture=neutral, 
                 PublicKeyToken=b03f5f7f11d50a3a"
                 restartOnExternalChanges="false" requirePermission="false" />
    </configSections>
    [...]
    <configBuilders>
        <builders>
            <add name="userSecrets" mode="Greedy" userSecretsId="User_secrets_folder_name" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </builders>
    </configBuilders>
    <appSettings configBuilders="userSecrets">
        <add key="AttemptsThreshold" value="3" />
        <add key="dbserver" value="placeholder"/>
        <add key="db" value="placeholder"/>
        <add key="dbuser" value="placeholder"/>
        <add key="dbpassword" value="placeholder"/>
        <add key="priodb" value="placeholder"/>
        <add key="pricingdb" value="placeholder"/>
        <add key="amazondb" value="placeholder"/>
    </appSettings>
    [...]
</configuration>
英文:

Edit: I had the wrong startup project selected so a different app.config was being used.

I moved to a new laptop today and I'm trying to set up my development environment again.

Our WPF client is using the ConfigurationManager to build the connection string for our DB. The app.config contains a few placeholders, but the actual credentials are stored in the user secrets. The configuration is being accessed like this:

server = ConfigurationManager.AppSettings[&quot;dbserver&quot;];
db = ConfigurationManager.AppSettings[&quot;db&quot;];
user = ConfigurationManager.AppSettings[&quot;dbuser&quot;];
pwd = ConfigurationManager.AppSettings[&quot;dbpassword&quot;];

On the old laptop this works fine. On the new laptop I get null values, not even the value of the placeholders. Any Idea, what I might have configured differently on the old laptop? Could the new laptop having windows 11 instead of windows 10 make a difference?

app.config

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;configuration&gt;
	&lt;configSections&gt;
		[...]	
		&lt;section name=&quot;configBuilders&quot;
				 type=&quot;System.Configuration.ConfigurationBuildersSection, 
			 System.Configuration, Version=4.0.0.0, Culture=neutral, 
			 PublicKeyToken=b03f5f7f11d50a3a&quot;
				 restartOnExternalChanges=&quot;false&quot; requirePermission=&quot;false&quot; /&gt;
	&lt;/configSections&gt;
	[...]
	&lt;configBuilders&gt;
		&lt;builders&gt;
			&lt;add name=&quot;userSecrets&quot; mode=&quot;Greedy&quot; userSecretsId=&quot;User_secrets_folder_name&quot; type=&quot;Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35&quot; /&gt;
		&lt;/builders&gt;
	&lt;/configBuilders&gt;
	&lt;appSettings configBuilders=&quot;userSecrets&quot;&gt;
		&lt;add key=&quot;AttemptsThreshold&quot; value=&quot;3&quot; /&gt;
		&lt;add key=&quot;dbserver&quot; value=&quot;placeholder&quot;/&gt;
		&lt;add key=&quot;db&quot; value=&quot;placeholder&quot;/&gt;
		&lt;add key=&quot;dbuser&quot; value=&quot;placeholder&quot;/&gt;
		&lt;add key=&quot;dbpassword&quot; value=&quot;placeholder&quot;/&gt;
		&lt;add key=&quot;priodb&quot; value=&quot;placeholder&quot;/&gt;
		&lt;add key=&quot;pricingdb&quot; value=&quot;placeholder&quot;/&gt;
		&lt;add key=&quot;amazondb&quot; value=&quot;placeholder&quot;/&gt;
	&lt;/appSettings&gt;
	[...]
&lt;/configuration&gt;

答案1

得分: -1

我是个傻瓜

我们的解决方案中有大约30个项目。我一直错误地将错误的项目设置为启动项目,这就是为什么使用了一个不包含我在这个问题中发布的部分的 app.config

英文:

I AM AN IDIOT

We have about 30 projects in the solution. I just kept using the wrong project as startup project, which is why an app.config was used that doesn't have the sections I posted in this question.

huangapple
  • 本文由 发表于 2023年5月25日 18:14:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定