Understanding .NET configuration file scheme
I have some difficulties with configuration files in .Net: I completely understand its essence and what are they good for, but the part I miss is this: configuration file is xml file that in runtime deserializes to managed object (or OBJECTS???) on the managed heap. But who are those objects?
To clarify my question, I will give one concrete example: If I write a WCF service, I can configure my service using the System.serviceModel element.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<!-- and this is wherer the service configuration goes... -->
</system.serviceModel>
</configuration>
Now, how can I access this开发者_Python百科 object using via code? what is the the object's name and type? I was thinking about ConfigurationManager - but its not there.
btw, if you wondering, the reason why I find this question so relevant is this: If I knew the scheme of the configuration file, I would know how to exploit it the best...
The root element corresponds to the Configuration
class and deserializes to it.
It contains many different other possible elements and some customisable sections, normally with the ConfigurationSection
class which one can subclass from and use.
The system.serviceModel
section is just one such section - you can find more information on the element itself here and about the object it deserializes to - ServiceModelConfigurationGroup
.
The ConfigurationManager
is a static class that provides access to the deserialized configuration (for example, you can get a configuration section by using the GetSection
method).
All the configuration sections are handled by "configuration section handlers". All of these need to be specified in the <configSections>
http://msdn.microsoft.com/en-us/library/ms228256.aspx element of the config file. If they are not defined there, they have already been defined in the machine.config or the global web.config file.
You can find System.Configuration.ConfigurationManager
as a class in the System.Configuration
DLL, which is part of the .NET framework (it's a bit confusing, because some of the System.Configuration namespace is already declared in the System
DLL, but to get the System.Configuration.ConfigurationManager
, you need to reference the System.Configuration.dll
.
精彩评论