c# - Call a constant name from web.config -
in webserivce coding, want call constant name web.config. have code :
namespace service.appcode.common.service { [servicecontract(namespace = constants.namespace)] public interface iservice1 { [operationcontract] void dowork(); } }
and code:
namespace service.appcode.common { public class constants { public const string namespace = configurationmanager.appsettings["defaulip"]; } }
it :
the expression being assigned 'service.appcode.common.constants.namespace' must constant
is possible call web.config?
constants defined @ compile-time , embedded directly resulting code, they're not initialized when application executes. (and can't change, unlike config value can change.)
however, looking @ this...
public class constants { public readonly static string namespace = configurationmanager.appsettings["defaulip"]; }
it looks want static immutable value. that's easy enough...
public class constants { public static string namespace { { return configurationmanager.appsettings["defaulip"]; } } }
that read value dynamically configuration time it's invoked. , since it's getter, it's read-only. if want read config once, cache value:
public class constants { private static string _namespace = null; public static string namespace { { if (_namespace == null) _namespace = configurationmanager.appsettings["defaulip"]; return _namespace; } } }
Comments
Post a Comment