Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4258538
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:39:46+00:00 2026-05-21T05:39:46+00:00

I am creating a windows service in .Net 4.0 and testing some functions of

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:39:47+00:00Added an answer on May 21, 2026 at 5:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a Windows Service in .NET to which N number of client
I am developing a .NET Windows service that is creating a couple of threads
I'm creating a C#.Net Windows Service and am wondering if you always have to
I'm going to be using Quartz.NET for a Windows Service I am creating. I
I am creating a Windows Service in C# that processes messages from a queue.
I am creating a windows service and want to know best practices for this.
I am creating a Windows service. When an exception occurrs, I handle it appropriately
I am creating windows installer project using Visual Studio 2005. Is there an option
I am creating a Windows Forms control derived from UserControl to be embedded in
We are creating a Windows Form application (C# or VB.NET) that needs to reference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.