Reading from configuration file within WCF web service
I'm trying to read from Constants.config
file within not ASP.NET enabled WCF web service.
Configuration rootWebConfig1 = Configuration.WebConfigurationManager.OpenWebConfiguration("Constants.config");
This call threw an exception, and it's seems like that OpenWebConfiguration
tries open the config file in some different location. How can I know where it looks for and how can I specify the 开发者_JS百科target folder?
Thanks a lot. Ilan.
OpenWebConfiguration
is intended to open a web configuration file - e.g. a web.config
only...
You can't use it to open your own config files, as far a I know.
You can however use the regular Configuration
class from the .NET System.Configuration
namespace. Check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject:
- Part 1: Unraveling the mysteries of .NET 2.0 configuration
- Part 2: Decoding the mysteries of .NET 2.0 configuration
- Part 3: Cracking the mysteries of .NET 2.0 configuration
Highly recommended, well written and extremely helpful!
In part 3, Jon shows how you can use your own custom config files and open them with the help of the ExeConfigurationFileMap
class:
ExeConfigurationFileMap exeMap = new ExeConfigurationFileMap();
exeMap.ExeConfigFilename = "C:\Application\Constants.config";
Configuration exeConfig =
ConfigurationManager.OpenMappedExeConfiguration(exeMap, ConfigurationUserLevel.None);
You can either do what marc_s said and use the Configuration (depreciated) or ConfigurationManager, or if you want you WCF service to support ASP.NET features you can turn on ASP.NET Compatability by decorating your service implementation with an AspNetCompatibilityRequirementsAttribute.
If the service will be tied to an ASP.NET site, always hosted in IIS, and would be making use of the ASP.NET session, this is probably the way to go. If not use the ConfigurationManager class.
精彩评论