App.config seems to be ignored

妖精的绣舞 提交于 2019-12-23 09:04:29

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4531506/app-config-seems-to-be-ignored

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!