473,383 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

AppSettings Section Being Returned Empty

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.AppSett ings
<Obsolete
System.Configuration.ConfigurationManager.AppSetti ngs

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.
Jun 27 '08 #1
6 5359
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/libr...64(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(Configur ationUserLevel.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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #2
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.

"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:T4**************@TK2MSFTNGHUB02.phx.gbl...
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/libr...64(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(Configur ationUserLevel.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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.


Jun 27 '08 #3
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(Configur ationUserLevel.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:
ms****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #4
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:
ms****@microsoft.com.

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

Jun 27 '08 #5
Hi Linda,

That's excellent, thank you for your help, that has solved the problem.

Nick.

"Linda Liu[MSFT]" <v-****@online.microsoft.comwrote in message
news:gy**************@TK2MSFTNGHUB02.phx.gbl...
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(Configur ationUserLevel.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:
ms****@microsoft.com.

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


Jun 27 '08 #6
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:
ms****@microsoft.com.

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

Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hazz | last post by:
I have spent more time than I care to admit trying to track down a very subtle error. Here is my app's xx.exe.config file. <?xml version="1.0" encoding="utf-8" ?> <configuration>...
3
by: Celebrate | last post by:
My application works fine when I run it locally but when I transfer it to our NEW Windows 2003 Web Edition server, I get a strange error. I've isolated the problem to when I issue a...
2
by: Fuehner | last post by:
Everyone-- I installed the .NET 2.0 framework on a server that also has version 1.1 installed. A few of my applications override the "appSettings" section in the web.config with a custom config...
1
by: Jenny | last post by:
Hello, I read my applications App.Config file using ConfigurationSettings.AppSettings When I try to check it's last access write time using File.GetLastWriteTime and the refresh new data...
1
by: Sergei Shelukhin | last post by:
Hi. Is is possible to divide appSetting in two, having some values stored in web.config itself and some in a section referenced in appSettings file="...." attribute?
1
by: Ken | last post by:
I'm currently using 1.1. Is there any way using the value of a ConfigurationSettings.AppSettings as a key of ConfigurationSettings.AppSettings.Tosting( )]? for example in web.config <add...
5
by: Osamede Zhang | last post by:
I just can't understand what appSettings means, how it work thanks for your read osamede
2
by: =?Utf-8?B?R29sZHdpbmQ=?= | last post by:
Hi, There is something fundamental which i donn't get it. I created a new Win Forms project in dotNet 2.0. I added Keys and Values to the settings. When I use...
1
by: =?Utf-8?B?THVib21pcg==?= | last post by:
Hi, I have a custom settings in <appSettingssection in the web.config. I need to modify this section from the C# exe application (NOT asp.net) web.config: <appSettings> <add key="MyKeyName"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.