Properties and configuration


J

Jan Eliasen

Hi

I am having some problems reading configuration values from a
configuration file, using C# 2.0.

I have programmed a Windows Service, and this part goes well - it runs
nicely.

Now, the Windows Service must call a Web Service every now and then. I
have added the web reference, and made it dynamic, so I can change the
URL later in the configuration file. This creates a Settings.settings
file, and an app.config file that reflects the values in the
Settings.settings file. If I add properties to the Settings.settings
file, they are also created for me in app.config and the other way
around. Great.

But if I read the properties in my code like this:
Properties.Settings.Default.Properties[DEBUGLOGGINGCONFIGKEY].DefaultValue.ToString();
then changes I make to app.config after deployment of the web service
don't work. The old values that were present at compile time are
always used.

If I try this:
ConfigurationManager.AppSettings[DEBUGLOGGINGCONFIGKEY]
then I just get null back.

In the old days (and in the .NET 2.0 days) I always use the
appSettings element under "configuration" to store applications
settings. But this time, because I made the web service dynamic,
something else has been created for me, and I can not seem to handle
it.

My app.config file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="thydata.dk.apm.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<thydata.dk.apm.Properties.Settings>
<setting name="QueuePath" serializeAs="String">
<value>.\ax_to_tbs</value>
</setting>
<setting name="thydata_dk_apm_TBS_TMHost"
serializeAs="String">
<value>http://localhost/TMHost Web Service/TMHost.asmx</value>
</setting>
<setting name="DebugLogging" serializeAs="String">
<value>True</value>
</setting>
</thydata.dk.apm.Properties.Settings>
</applicationSettings>
</configuration>

Can anyone tell me how I will get to the DebugLogging value at runtime
in my code?

Thanks!

--
eliasen, representing himself and not the company he works for.

Private blog: http://blog.eliasen.dk

Private email: (e-mail address removed)
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

But if I read the properties in my code like this:
Properties.Settings.Default.Properties[DEBUGLOGGINGCONFIGKEY].DefaultValue.ToString();
then changes I make to app.config after deployment of the web service
don't work. The old values that were present at compile time are
always used.


You're asking for the *default value*. Just use

Properties.Settings.Default.DebugLogging

The "default" part here just means "use the settings which are
*loaded* by default" which isn't the same as the default values of
those settings :)

Jon
 
J

Jan Eliasen

You're asking for the *default value*. Just use

Properties.Settings.Default.DebugLogging

The "default" part here just means "use the settings which are
*loaded* by default" which isn't the same as the default values of
those settings :)
I just KNEW there had to be something I just didn't get... I have
spend the better part of 2 days searching for examples that matched my
setup, but couldn't find it...

Thanks, Jon!

--
eliasen, representing himself and not the company he works for.

Private blog: http://blog.eliasen.dk

Private email: (e-mail address removed)
 
Ad

Advertisements

L

Linda Liu [MSFT]

Hi Jan,

Application Settings introduced in .NET 2.0 is different from the app
settings in .NET 1.x.

The schemas of the app.config file of .NET 2.0 and .NET 1.x are different,
as well as the programming models for them.This is why you get a null value
when you call 'ConfigurationManager.AppSettings[DEBUGLOGGINGCONFIGKEY]' to
get an application setting in .NET 2.0.

To get the current value of an application setting, you could just use the
Properties.Settings class as follows:

string settingValue =
Properties.Settings.Default.Properties[DEBUGLOGGINGCONFIGKEY];

For more informaton on Application Settings, you may refer to the following
MSDN documents:

http://msdn2.microsoft.com/en-us/library/a65txexh(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx

BTW, you have mentioned you read the application setting in you code like
this:
Properties.Settings.Default.Properties[DEBUGLOGGINGCONFIGKEY].DefaultValue.T
oString();

But I don't see object has the property 'DefaultValue'.

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

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top