为什么 ConfigurationManager.OpenMappedExeConfiguration 有一个额外的连接字符串不在文件中?

发布于 2024-08-17 05:13:33 字数 2774 浏览 12 评论 0原文

我正在对 win 表单应用程序的配置文件进行单元测试。 在 LocalTestRun.testrunco​​nfig 中,我将其设置为复制 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 技术交流群。

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

发布评论

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

评论(2

七堇年 2024-08-24 05:13:33

该连接字符串在机器级配置文件 machine.config 中定义。

如果您查看文件 C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config,您将看到以下部分:

<connectionStrings>
    <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

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:

<connectionStrings>
    <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
小耗子 2024-08-24 05:13:33

戴夫明确了主要答案 - 赞成。

但是:如果您想从 machine.config 或任何“上游”web.config 中删除任何连接字符串,您始终可以使用

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <clear />
        <add name="HLIT_Ticketing.Properties.Settings.HomeQConnectionString"
             connectionString="Data Source=******\*****;Initial Catalog=*****;Persist Security Info=True;User ID=*****;Password=******"
             providerName="System.Data.SqlClient" />
    </connectionStrings>

:连接字符串部分会清除任何以前存在的连接字符串并从头开始。

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:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <clear />
        <add name="HLIT_Ticketing.Properties.Settings.HomeQConnectionString"
             connectionString="Data Source=******\*****;Initial Catalog=*****;Persist Security Info=True;User ID=*****;Password=******"
             providerName="System.Data.SqlClient" />
    </connectionStrings>

That first <clear/> in the connection strings section wipes out any previously present connection strings and starts with a clean slate.

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