AppSettings Section Being Returned Empty

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

Hi there,

I have a Windows Service application that has a load of settings
defined, I can access these using My.Settings.

I would like to pass the NameValueCollection of the settings to another
class but unfortunately no matter what I try I get an empty colleciton back,
i.e.

System.Configuration.ConfigurationSettings.AppSettings
<Obsolete
System.Configuration.ConfigurationManager.AppSettings

Now I know the settings are there, this code was working perfect as a
standard executable, but since I changed it to a Windows Service, it just
stopped working, any idea where my settings have gone and how I can get the
NameValueCollection?

Thanks in advance.

Nick.
 
Hi Nick,

Based on my understanding, you read AppSettings in a Windows Service
application but the NameValueCollection of the settings returns empty. The
same code works perfect in a WinForm desktop application. If I'm off base,
please feel free to let me know.

I performed a test based on your description but didn't reproduce the
problem on my side.

I follow the instructions in the following MSDN document to create a
Windows Service application:
http://msdn.microsoft.com/en-us/library/aa984464(VS.71).aspx

I add an Application Configuration File in the project and add an
appSettings in the app.config file:
<configuration>
<appSettings>
<add key="appsetting1" value="value1"/>
<add key="appsetting2" value="value2"/>
</appSettings>
...
</configuration >

In the override OnStart method, I add the following lines of code:

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.

Dim config As Configuration =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim col As KeyValueConfigurationCollection =
config.AppSettings.Settings
Dim valuestr As String
valuestr = col.Item("appsetting1").Value & " " &
col.Item("appsetting2").Value
EventLog1.WriteEntry("In OnStart:" & valuestr)

End Sub

Build and install the Windows Service application. When the service is
started, an entry is written to the MyNewLog "In OnStart:value1 value2".

Is there any difference between your code and mine?

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Linda,

Indeed there are differences between your code and mine. I have added
my settings through the project menu and let the IDE which has not produced
a settings element that looks anything like yours... e.g.

<applicationSettings>
<SomeApp.My.MySettings>
<setting name="Port" serializeAs="String">
<value>1001,1002</value>
</setting>
<setting name="LogFilePath" serializeAs="String">
<value>c:\logs\</value>
</setting>
<setting name="EnableSounds" serializeAs="String">
<value>True</value>
</setting>
</SomeApp.My.MySettings>
</applicationSettings>

As I mentioned in my previous post, I can access the settings through
the My namespace. So are you suggesting that I need 2 lots of settings? I
would rather just stick with the settings the way I have them, but if that
is the only way then I shall create another settings section, any ideas?

Thanks for your time.

Nick.
 
Hi Nick,

Thank you for your quick response!

Since you add application-scoped settings in the Settings.settings under
the My Project folder, you cannot access these settings through
ConfigurationManager.AppSettings property.

In your scenario, the application-scoped settings are in a section named
"SomeApp.My.MySettings" under a section group named "applicationSettings".
To get these settings using ConfigurationManager, we need to get the
section group first and then get the section. The following is a sample:

Dim config As Configuration =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

' get the section group named "applicationSettings"
Dim secGroup As ConfigurationSectionGroup =
config.SectionGroups("applicationSettings")

' get the section named "SomeApp.My.MySettings"
Dim applicationSec As ClientSettingsSection =
CType(secGroup.Sections("SomeApp.My.MySettings"), ClientSettingsSection)

' get the settings of the section
Dim applicationSettingCol As SettingElementCollection =
applicationSec.Settings

' enumerate the element in the settings
For Each element As SettingElement In applicationSettingCol
Console.WriteLine(element.Name & " " & element.Value.ValueXml.Value)
Next

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Nick,

How about the problem now? Have you had a chance to try my suggestion?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Nick,

Thank you for your feedback! I'm glad to hear that the problem is solved
now.

If you have any questions in the future, please don't hesitate to contact
us. It's always our pleasure to be of assistance!

Have a nice day!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top