6

I've this Class Library, as a result of a refactor action. I added an App.config file and added something like this:

<configuration>
    <connectionStrings>
        <add name="MyDatabase" connectionString="Data Source=server;Initial Catalog=database;User ID=userid;Password=password" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

But when I run the application, debugging learns me this is totally ignored. The immediate window tells me:

ConfigurationManager.ConnectionStrings[0]
                 {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
    base {System.Configuration.ConfigurationElement}: {data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
    ConnectionString: "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
    Key: "LocalSqlServer"
    Name: "LocalSqlServer"
    Properties: {System.Configuration.ConfigurationPropertyCollection}
    ProviderName: "System.Data.SqlClient"

I've checked generated config file in the bin directory and its contents are identical to the App.config.

I try to read the App.config using:

ConfigurationManager.ConnectionStrings[Constants.Connections.DevConnection].ConnectionString

Nothing out of the ordinary I'd say, but what is going wrong?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Oxymoron
  • 1,382
  • 4
  • 20
  • 40
  • Check this question: http://stackoverflow.com/questions/4472232/configuration-from-app-config-isnt-being-pulled-correctly/4472261#4472261 – Xaqron Dec 25 '10 at 20:35

3 Answers3

8

A class library doesn't get its own config; for an app named Foo.exe you need your configuration to be in Foo.exe.config. The exception here is web apps, where web.config is the naming convention.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
5

This connectionString should be in the corresponding app.config of the exe that you are running.

wazz
  • 4,953
  • 5
  • 20
  • 34
Chandu
  • 81,493
  • 19
  • 133
  • 134
0

Is the file being copied to output? Secondly the connection string should be accessible from the AssemblyName.Settings.Properties class which is generated from your code.

Spence
  • 28,526
  • 15
  • 68
  • 103