App.config 似乎被忽略
作为重构操作的结果,我有了这个类库。 我添加了一个 App.config 文件并添加了如下内容:
<configuration>
<connectionStrings>
<add name="MyDatabase" connectionString="Data Source=server;Initial Catalog=database;User ID=userid;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
但是当我运行应用程序时,调试发现这完全被忽略了。立即窗口告诉我:
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"
我已经检查了 bin 目录中生成的配置文件,其内容与 App.config 相同。
我尝试使用以下方式读取 App.config:
ConfigurationManager.ConnectionStrings[Constants.Connections.DevConnection].ConnectionString
我想说没有什么异常,但是出了什么问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
类库没有自己的配置;对于名为 Foo.exe 的应用程序,您需要将配置放在 Foo.exe.config 中。这里的例外是 Web 应用程序,其中 web.config 是命名约定。
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.
此连接字符串应该位于您正在运行的 exe 的相应 app.config 中。
This connectionString should be in the corresponding app.config of the exe that you are running.
文件是否被复制到输出?其次,应该可以从代码生成的 AssemblyName.Settings.Properties 类访问连接字符串。
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.