如何从App.config中在.NET 4.0中使用ConfigurationManager中读取值?config、App、NET、ConfigurationManager

2023-09-08 09:31:21 作者:夕阳下的孤影

我创建一个Windows服务在.NET 4.0中,并通过引用服务项目检测,上述服务的一些功能与Windows窗体客户端。

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.

服务项目有一个app.config文件,该文件是这样的:

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的

空引用错误被抛出,因为没有加载我的连接字符串。装载了唯一的ConnectionStrings来自于c machine.config文件:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ CONFIG \ machine.config的

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

如果我创建一个应用程序范围设置,服务,我可以使用My.Settings.setting得到该设置 - >所以它不是像App.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.

我的问题是:为什么我的ConnectionStrings未从App.config中加载的文件

感谢您的帮助。

更新:

此外,在这一点上,连周围的工作将是AP preciated;使用的app.config的唯一原因是为了能够使用DpapiProtectedConfigurationProvider的内容进行加密(内容将对服务和数据库连接一些用户名/密码的值)。

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).

我试图在App.config手动创建的AppSettings部分,但这些设置也不会被ConfigurationManager中(计数= 0)。

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

更新2:

每一个建议,我试图手动打开app.config文件像这样:

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)

因此​​,这里是怪异的一部分,当我进去看看,路径是正确的(指向我的app.config),但的ConnectionStrings仍在从machine.config文件加载(我的ConnectionStrings不加载)!哎呀

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

更新3:

好了,所以,我想它了。当引用从另一个项目(子)项目(父),孩子的的app.config是即使正在使用父类中使用。因此,我可以得到的ConnectionStrings展现出来了,如果我将它们复制到孩子的app.config中。当试图手动打开它,我的currentDirectory所是孩子,而不是父(奇怪,怎么也没有抛出一个异常 - 它不会已经能够找到的配置文件...它只是默默地用在machine.config ... 好吧)。

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).

谢谢大家的帮助!

推荐答案

你会想要做的第一件事是确保该服务帐户可以访问文件(如果没有运行的系统)。这听起来像,但因为你提到My.Settings.Setting工作,首先应该没问题。

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.

另一件事看出来的是,以确保在app.config中有该服务的可执行文件的名字 - 所以,如果服务的exe是MyService.exe在app.config必须命名为MyService.exe.config

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.

最后要记下:图书馆将从可执行文件的app.config中加载库,而不是在app.config是与图书馆阅读。所以,如果你有服务可执行项目为MyService 和库 MyServiceLibrary 的code在一个项目该库将读取在app.config 为MyService 不是 MyServiceLibrary

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.