为什么 ConfigurationManager.OpenMappedExeConfiguration 有一个额外的连接字符串不在文件中?
我正在对 win 表单应用程序的配置文件进行单元测试。 在 LocalTestRun.testrunconfig 中,我将其设置为复制 app.config。我在测试运行时检查了 Environment.CurrentDirectory
并且该文件也没有这个额外的连接字符串。这是测试方法:
[TestMethod]
public void Configuration_ConnectionStrings_Connect()
{
Console.WriteLine(Environment.CurrentDirectory);
Assert.IsTrue(System.IO.File.Exists("App.Config"));
var configFileMap = new ExeConfigurationFileMap(){ ExeConfigFilename = "App.Config"};
var config= ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
foreach (ConnectionStringSettings connectionString in config.ConnectionStrings.ConnectionStrings)
{
//Assumes all connections are to Sql server, test must be updated if not
Assert.AreEqual("System.Data.SqlClient",connectionString.ProviderName,"Test was only designed for SQL clients");
using (var cn=new System.Data.SqlClient.SqlConnection(connectionString.ConnectionString))
{
cn.Open();
Assert.AreEqual(System.Data.ConnectionState.Open,cn.State);
}
}
}
但是,当我迭代 config.ConnectionStrings.ConnectionStrings 时,我有一个不在文件中的额外配置字符串:
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
这不是也从来不是 ASP.NET 项目。
这是配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="HLIT_Ticketing.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="HLIT_Ticketing.Properties.Settings.HomeQConnectionString"
connectionString="Data Source=******\*****;Initial Catalog=*****;Persist Security Info=True;User ID=*****;Password=******"
providerName="System.Data.SqlClient" />
</connectionStrings>
<userSettings>
<HLIT_Ticketing.Properties.Settings>
<setting name="AssociateRole" serializeAs="String">
<value>Associate</value>
</setting>
<setting name="DeveloperRole" serializeAs="String">
<value>Developer</value>
</setting>
</HLIT_Ticketing.Properties.Settings>
</userSettings>
这个额外的连接字符串从哪里来?
I'm unit testing my config file for a win forms application.
In the LocalTestRun.testrunconfig I set it to copy the app.config. I've checked the Environment.CurrentDirectory
while the test was running and the file doesn't have this extra connection string either. This is the test method:
[TestMethod]
public void Configuration_ConnectionStrings_Connect()
{
Console.WriteLine(Environment.CurrentDirectory);
Assert.IsTrue(System.IO.File.Exists("App.Config"));
var configFileMap = new ExeConfigurationFileMap(){ ExeConfigFilename = "App.Config"};
var config= ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
foreach (ConnectionStringSettings connectionString in config.ConnectionStrings.ConnectionStrings)
{
//Assumes all connections are to Sql server, test must be updated if not
Assert.AreEqual("System.Data.SqlClient",connectionString.ProviderName,"Test was only designed for SQL clients");
using (var cn=new System.Data.SqlClient.SqlConnection(connectionString.ConnectionString))
{
cn.Open();
Assert.AreEqual(System.Data.ConnectionState.Open,cn.State);
}
}
}
However When I iterate the config.ConnectionStrings.ConnectionStrings
I have an extra configuration string that is not in the file:
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
This isn't and never was an asp.net project.
Here's the config File:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="HLIT_Ticketing.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="HLIT_Ticketing.Properties.Settings.HomeQConnectionString"
connectionString="Data Source=******\*****;Initial Catalog=*****;Persist Security Info=True;User ID=*****;Password=******"
providerName="System.Data.SqlClient" />
</connectionStrings>
<userSettings>
<HLIT_Ticketing.Properties.Settings>
<setting name="AssociateRole" serializeAs="String">
<value>Associate</value>
</setting>
<setting name="DeveloperRole" serializeAs="String">
<value>Developer</value>
</setting>
</HLIT_Ticketing.Properties.Settings>
</userSettings>
Where is this extra connection string coming from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该连接字符串在机器级配置文件 machine.config 中定义。
如果您查看文件
C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config
,您将看到以下部分:That connection string is defined in the machine-level configuration file, machine.config.
If you look at the file
C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config
, you will see the following section:戴夫明确了主要答案 - 赞成。
但是:如果您想从 machine.config 或任何“上游”web.config 中删除任何连接字符串,您始终可以使用
:连接字符串部分会清除任何以前存在的连接字符串并从头开始。
Dave nailed the main answer - upvoted.
But: if you want to get rid of any connection string from the machine.config or any "up-stream" web.configs, you can always use:
That first
<clear/>
in the connection strings section wipes out any previously present connection strings and starts with a clean slate.