Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
  </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!

like image 744
Dima Avatar asked Apr 10 '11 00:04

Dima


1 Answers

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.

like image 167
Joshua Avatar answered Oct 22 '22 13:10

Joshua