views:

307

answers:

2

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?

+5  A: 

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>
Dave Cluderay
Is there a way to read the app.config in isolation rather than loading the app.config as an xml file?
Maslow
No, sorry - it is as marc_s says!
Dave Cluderay
+4  A: 

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.

marc_s
Is there a way to tell the configuration manager to load only the file I specify for testing?
Maslow
I want to test the app's configuration file, not the machine's.
Maslow
No, the ASP.NET config system is already a hierarchy from machine.config, your "root" web.config, and any potential "upstream" web.configs. You cannot change that, it's such a fundamental mechanism.
marc_s
so... is there a way through the configuration namespace to read only the app.config and not the machine.config? or am I left to use Linq to XML for this testing?
Maslow
The .NET config story is as I said - it's **always** the hierarchy from machine.config -> "root" web.config -> your web.config or machine.config -> yourapp.exe.config. You cannot change that (other than reading the XML yourself, or using the <clear/> tags as I mentioned)
marc_s