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

38,658

Solution 1

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.

Solution 2

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?

Share:
38,658
Dima
Author by

Dima

Updated on February 25, 2020

Comments

  • Dima
    Dima about 4 years

    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!

    • František Žiačik
      František Žiačik about 13 years
      May be a silly question, but how do you deploy the service and its app.config? Do you have a servicename.exe.config in the correct directory?
    • Dima
      Dima about 13 years
      I have not deployed it yet ... all is still in debugging on my local machine. When I deploy it, I will be sure that the config file ends up in the System32 folder (I think) or whatever folder that windows services look for files by default.
  • Dima
    Dima about 13 years
    It is ... and as I mentioned, if I create a setting, that setting is retrieved without a problem.
  • Dima
    Dima about 13 years
    Yes to all accounts. Named properly (I mean, it is auto named when compiled and I verified that the name is correct in the debug folder). I thought about which App.config would be used and came to the same conclusion as you outlined - i.e. the service app.config is the one that is used. I am still beating my head against the wall as to why my connectionStrings are not being read ... sigh
  • Diego Mijelshon
    Diego Mijelshon about 13 years
    @Dima: user settings are not stored in app.config
  • Dima
    Dima about 13 years
    What do you mean? All that is in the config file is what I have pasted into the question. Actually, I am glad you mentioned this as I am not setting the providerName for the connection string ... is this a necessary value? If it is, what do I set it to?
  • Hogan
    Hogan about 13 years
    @dima nm about previous answer -- try the comment above.
  • Dima
    Dima about 13 years
    Thank you for the link; it looks like I do not need a providerName.
  • Dima
    Dima about 13 years
    That is the full config file right now for testing purposes ... really, there is nothing else in it ... what you see is what is in my config file. Now, as far as opening the file manually, I have edited my question to include the details of that ... thank you ... please see above for details.
  • Dima
    Dima about 13 years
    Oh, and thank you for mentioning re-adding the reference but that is the first thing I tried.
  • Dima
    Dima about 13 years
    I guess I misread your comment/answer initially, but you were correct. Thank you.
  • Dima
    Dima about 13 years
    This was the closest to the actual answer but I commented in my question to make it clearer (I think, I hope).