Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.config seems to be ignored

Tags:

c#

app-config

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?

like image 932
Oxymoron Avatar asked Dec 25 '10 20:12

Oxymoron


2 Answers

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.

like image 58
Marc Gravell Avatar answered Oct 02 '22 02:10

Marc Gravell


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

like image 43
Chandu Avatar answered Oct 02 '22 00:10

Chandu