如何使用configurationManager从.Net 4.0中的App.config读取值?

发布于 2024-10-31 06:31:37 字数 1724 浏览 0 评论 0原文

我正在 .Net 4.0 中创建一个 Windows 服务,并通过引用该服务项目使用 Windows 窗体客户端测试该服务的一些功能。

该服务项目有一个 App.config 文件,该文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
     <connectionStrings>
       <clear />
       <add name="myLocalMySQLDBUsername" connectionString="username"/>
     </connectionStrings>
  </configuration>

当属于该服务的函数调用时:

  • ConfigurationManager.ConnectionStrings("myLocalMySQLDBUsername").ConnectionString

会引发空引用错误,因为我的连接字符串未加载。加载的唯一连接字符串来自位于 c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config\machine.config 中的 machine.config 文件

如果我为服务创建应用程序范围设置,我可以得到使用 My.Settings.setting -> 设置所以这并不是说 App.config 文件没有被读取。

我的问题是:为什么我的connectionStrings没有从App.config文件中加载?

感谢您的帮助。

更新:

此外,在这一点上,即使是变通办法也将受到赞赏;使用 app.config 的唯一原因是能够使用 DpapiProtectedConfigurationProvider 加密内容(内容将具有一些用于服务和数据库连接的用户名/密码值)。

我尝试在 app.config 中手动创建 AppSettings 部分,但配置管理器也未读取这些设置(计数 = 0)。

更新2:

根据建议,我尝试手动打开app.config文件,如下所示:

Dim exePath As String = System.IO.Path.Combine(Environment.CurrentDirectory, "ServiceName.exe")

Dim myConfig As Configuration = ConfigurationManager.OpenExeConfiguration(exePath)

所以这是奇怪的部分,当我查看内部时,路径是正确的(指向我的app.config)但连接字符串仍在从 machine.config 文件中加载(我的连接字符串未加载)! ARGH

更新3:

好吧,所以,我想通了。当从另一个项目(子项目)引用一个项目(父项目)时,即使正在使用父项目的类,也会使用子项目的 app.config 。因此,如果我将连接字符串复制到孩子的 app.config 中,我就可以显示它们。当尝试手动打开它时,我的 currentDirectory 是子目录,而不是父目录(奇怪的是它没有抛出异常 - 它无法找到配置文件......它只是默默地使用 machine.config ... 那好吧)。

感谢大家的帮助!

I am creating a windows service in .Net 4.0 and testing some functions of said service with a windows forms client by referencing the service project.

The service project has an App.config file and that file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
     <connectionStrings>
       <clear />
       <add name="myLocalMySQLDBUsername" connectionString="username"/>
     </connectionStrings>
  </configuration>

When a function belonging to the service calls:

  • ConfigurationManager.ConnectionStrings("myLocalMySQLDBUsername").ConnectionString

a null reference error is thrown because my connection string is not loaded. The only connectionStrings that are loaded are from the machine.config file located in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config\machine.config

If I create an application scope setting for the service, I can get that setting by using the My.Settings.setting -> so it's not like the App.config file is not being read.

My question is: why are my connectionStrings not being loaded from the App.config file?

Thank you for your help.

UPDATE:

Also, at this point, even a work around would be appreciated; the only reason for using app.config is to be able to encrypt the contents using the DpapiProtectedConfigurationProvider (the contents will have some username/password values for service and database connections).

I tried creating an AppSettings section manually in the app.config but those settings were also not read by the configurationManager (count = 0).

UPDATE 2:

Per a suggestion, I tried to manually open the app.config file like so:

Dim exePath As String = System.IO.Path.Combine(Environment.CurrentDirectory, "ServiceName.exe")

Dim myConfig As Configuration = ConfigurationManager.OpenExeConfiguration(exePath)

So here is the weird part, when I look inside, path is correct (points to my app.config) but the connectionStrings are still being loaded from the machine.config file (my connectionStrings are not loaded)!! ARGH

UPDATE 3:

Okay, so, I figured it out. When referencing a project(parent) from another project(child), the child's app.config is used even if the parent's classes are being used. Thus, I can get the connectionStrings to show up if I copy them over to the child's app.config. When trying to open it manually, my currentDirectory was of the child, not the parent (strange how it did not throw an exception - it wouldn't have been able to find the config file ... it just silently used the machine.config ... oh well).

Thanks all for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白鸥掠海 2024-11-07 06:31:37

您要做的第一件事是确保服务帐户有权访问该文件(如果不作为系统运行)。听起来应该没问题,因为你提到 My.Settings.Setting 有效。

另一件需要注意的事情是确保 app.config 中包含服务可执行文件的名称 - 因此,如果服务 exe 是 MyService.exe,则 app.config 必须命名为 MyService.exe.config。

最后要注意的是:库将从加载库的可执行文件的 app.config 中读取,而不是从库附带的 app.config 中读取。因此,如果您有一个服务可执行文件 MyService 的项目和一个库 MyServiceLibrary 的项目,则库中的代码将从 MyService 读取 app.config代码>而不是MyServiceLibrary

The first thing you'll want to do is make sure that the service account has access to the file (if not running as SYSTEM). It sounds like it should be ok though since you mention My.Settings.Setting works.

The other thing to look out for is to make sure that the app.config has the name of the service executable in it - so if the service exe is MyService.exe the app.config must be named MyService.exe.config.

The last thing to make note of: libraries will read from the executable's app.config that loads the library, not the app.config that is with the library. So if you have a project for the service executable MyService and a project for the library MyServiceLibrary the code in the library will read the app.config from MyService not MyServiceLibrary.

梦年海沫深 2024-11-07 06:31:37

我看到有些人说这个问题可以通过手动重新添加对 System.Configuration.dll 的引用来解决

旁注:如果这确实是整个 app.config 文件而不仅仅是一个片段,那么是你的问题...你的 app.config 文件应该更复杂,否则 .NET 将无法加载它。

解决方法:使用配置管理器打开此配置文件(有一个 API。)您无法让它自动加载,只需告诉配置管理器打开它即可。

我仍然认为问题是你的配置文件无效——你能发布完整的文件吗?

I saw some people say this problem might be fixed by manually re-adding a reference to System.Configuration.dll

SIDE NOTE: If that really is you whole app.config file and not just a snippet then that is your problem... you're app.config file should be MUCH more complicated or .NET will not be able to load it.

WORK AROUND: Use the configuration manager to open this config file (there is an API for that.) You can't get it to load auto-magically just tell the config manager to open it.

I still think the problem is your config file is invalid -- could you please post the FULL file?

最冷一天 2024-11-07 06:31:37

确保配置文件部署到与可执行文件相同的文件夹,并且其名称为 your. assembly.exe.config

Make sure the config file is deployed to the same folder as the executable file, and that it's called your.assembly.exe.config.

薄凉少年不暖心 2024-11-07 06:31:37

我遇到了类似的问题,但就我而言,这是因为我更改了项目的名称空间。这也用作配置文件中的应用程序设置部分元素名称,因此代码未找到新的部分名称。摆弄项目属性中的自定义设置值之一并重建会导致新部分与旧部分一起写入 app.config ,这就是向我表明问题的原因。

I had a similar problem, but in my case it was because I had changed the project's namespace. This is also used as the application settings section element name in the config file, so the code was not finding the new section name. Fiddling with one of the custom setting's values in the project properties and rebuilding caused the new section to written into the app.config alongside the old ones which was what indicated the issue to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文