开发者

How to read values from App.config in .Net 4.0 using configurationManager?

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>
  </conf开发者_开发问答iguration>

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!


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.


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?


Make sure the config file is deployed to the same folder as the executable file, and that it's called your.assembly.exe.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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜