PC Review


Reply
Thread Tools Rate Thread

Baffling static property issue.

 
 
Steve
Guest
Posts: n/a
 
      16th Mar 2005
I'm currently running into a problem, and I have no idea what to make of it.

I have a class with nested clases and static properties that I'm using
to store configuration information using a custom configuration section.
When my code accesses one of the static properties the static
constructors are fired, and the code goes in and pulls the data from the
configuration section.

In the current instance, I'm getting a connection string. The call is
made to the static property. When the code returns the property value,
it is an empty string. If I type
Config.ConnectionStrings.AuthenticatedUser in the Command Window while
debugging the proper connection string value is present.

I have tried placing multiple calls to the property in my code to see if
something wasn't be initialized properly on the first call.
Unfortunately the behavior is the same everytime. Call from code, get an
empty string. Call from the Command Window, get the expected value.

Does anyone have any idea what might be going on here? Below is the code
that I'm using.

Thanks,
Steve


public class Config {

static Config() {
object config = ConfigurationSettings.GetConfig("SystemConfig");

if (config == null) {
throw new ConfigurationException("No SystemConfig section was
specified.");
}
}

public object Create(object parent, object configContext,
System.Xml.XmlNode section) {
//Get the connection strings
string authenticatedUser = ConfigurationParser.GetAttribute(section,
"AuthenticatedUser", true);
string unauthenticatedUser =
ConfigurationParser.GetAttribute(section, "UnauthenticatedUser", true);
ConnectionStrings.SetConnectionStrings(authenticatedUser,
unauthenticatedUser);

return true;
}

internal static void Load() {}

public class ConnectionStrings {
private static string _AuthenticatedUser = "";
private static string _UnauthenticatedUser = "";

static ConnectionStrings() {
//Used to get the static Config constructor to fire.
Config.Load();
}

internal static void SetConnectionStrings(string authenticatedUser,
string unauthenticatedUser) {
_AuthenticatedUser = authenticatedUser;
_UnauthenticatedUser = unauthenticatedUser;
}

public static string AuthenticatedUser {
get {return _AuthenticatedUser;}
}

public static string UnauthenticatedUser {
get {return _UnauthenticatedUser;}
}
}
}
 
Reply With Quote
 
 
 
 
Chris R. Timmons
Guest
Posts: n/a
 
      16th Mar 2005
Steve,

Config.Create, which does the actual loading of the config data, is
never called by either Config or ConnectionStrings.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      16th Mar 2005
Chris R. Timmons wrote:
> Steve,
>
> Config.Create, which does the actual loading of the config data, is
> never called by either Config or ConnectionStrings.
>


Config.Create does not need to be called manually. It is called when
ConfigurationSettings.GetConfig(string) is called, dependent on what is
configured in the application's config file, in the configSections block.

I'm able to watch the code go through the Create routine everytime while
in the debugger, so that doesn't appear to be the problem.

Steve
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      16th Mar 2005
Steve <(E-Mail Removed)> wrote:
> I'm currently running into a problem, and I have no idea what to make of it.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Chris R. Timmons
Guest
Posts: n/a
 
      17th Mar 2005
Steve <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Chris R. Timmons wrote:
>> Steve,
>>
>> Config.Create, which does the actual loading of the config
>> data, is never called by either Config or ConnectionStrings.
>>

>
> Config.Create does not need to be called manually. It is called
> when ConfigurationSettings.GetConfig(string) is called,
> dependent on what is configured in the application's config
> file, in the configSections block.
>
> I'm able to watch the code go through the Create routine
> everytime while in the debugger, so that doesn't appear to be
> the problem.


Steve,

Sorry about that. I'm not as well informed about dynamic
configuration properties as I like to think I am :-).

I believe the custom section handler class Config needs to explicitly
declare it is implementing IConfigurationSectionHandler in order for
its Create method to be called. Something like this:

public class Config : IConfigurationSectionHandler


--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: Static Method Instantiating Itself To Acess Non-static Property's. Ok? Electric Co. Microsoft ASP .NET 3 9th Aug 2006 09:32 PM
Baffling .NET Threading issue. paul.driegen@gmail.com Microsoft Dot NET 2 2nd Sep 2005 11:00 PM
Complex CD-R issue that is baffling? =?Utf-8?B?QWxhbg==?= Windows XP Help 1 9th Nov 2004 12:54 PM
Why can't I implement an interface property as a static property? =?Utf-8?B?RXJpayBIYXJyaXM=?= Microsoft C# .NET 3 19th Jul 2004 07:28 PM
Baffling Wireless Issue... Ringo Windows XP General 4 2nd Jul 2004 01:30 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:51 AM.