Baffling static property issue.

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

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;}
}
}
}
 
Steve,

Config.Create, which does the actual loading of the config data, is
never called by either Config or ConnectionStrings.
 
Chris said:
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
 
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
 
Back
Top