Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ConfigurationManager.OpenMappedExeConfiguration have an extra connection string not in the file?

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?

like image 252
Maslow Avatar asked Jan 08 '10 16:01

Maslow


1 Answers

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>
like image 183
Dave Cluderay Avatar answered Nov 03 '22 20:11

Dave Cluderay