By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,948 Members | 1,545 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,948 IT Pros & Developers. It's quick & easy.

Baffling static property issue.

P: n/a
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(authenticat edUser,
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;}
}
}
}
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/
Nov 16 '05 #2

P: n/a
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
Nov 16 '05 #3

P: n/a
Steve <ng*****@digitalnothing.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
Steve <ng*****@digitalnothing.com> wrote in
news:eB**************@TK2MSFTNGP09.phx.gbl:
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/
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.