custom sectionGroups in web.config?
-
Hi all. I have a webservice coded in c#. I would like to add some custom sectiongroups to the web.config file, and I manage to do so in a normal application (to the App.config file), but have run into problems with the system.web part of the web.config file. I try to declare system.web as another sectiongroup (as I have seen on some pages on the net) but the web.config file below gives me this error: "Section or group name 'sessionState' has already been defined" If I on the other hand does not declare sessionState (or some other "entry" in the system.web sectiongroup) I get this error when trying to consume the service "Could not load type System.Configuration.NameValueSectionHandler from assembly System.Web"
-
Hi all. I have a webservice coded in c#. I would like to add some custom sectiongroups to the web.config file, and I manage to do so in a normal application (to the App.config file), but have run into problems with the system.web part of the web.config file. I try to declare system.web as another sectiongroup (as I have seen on some pages on the net) but the web.config file below gives me this error: "Section or group name 'sessionState' has already been defined" If I on the other hand does not declare sessionState (or some other "entry" in the system.web sectiongroup) I get this error when trying to consume the service "Could not load type System.Configuration.NameValueSectionHandler from assembly System.Web"
Hi, If you just want to do name/value collection, then why not use the <appsetting> tags, eg.
<system.web>
...
</system.web><!-- Generic application settings -->
<appSettings>
<add key="MyKey" value="MyValue" />
</appSettings>Then use the following to access them:
System.Configuration.ConfigurationSettings.AppSettings["MyKey"]
But to answer your question about Custom config handlers, you need to create an object that implements the IConfigurationSectionHandler interface, e.g.
namespace MyCompany.MySystem.Web.MyProduct.Configuration.Custom
{
internal class CustomConfigHandler : IConfigurationSectionHandler
{
#region Custom Config Handler methodspublic virtual object Create(Object parent, Object context, XmlNode node) { return new CustomConfig((CustomConfig)parent, node); } #endregion } public class CustomConfig { internal CustomConfig(CustomConfig parent, XmlNode node) { // implement how you want to parse the XML config } // methods/properties to access information // ... // ... // ... }
}
So the system calls our CustomConfigHandler object passing the XML contents of the config file, and it's up to us to parse this. I generally create an object (CustomConfig) that stores this XML and exposes methods/properties that the web application can call to get the details. The methods/properties simply perform SelectSingleNode(XPATH) on the stored XML to retrieve the details. This allows for a more complex config structure than simple name/value pairs, e.g. Here is a snippet of one of my custom config sections...
<!-- Custom application configuration -->
<osi.search.types>
<Type name="VIF">
< Provider type='CSV'>
<Source>
<!--
[Location] can be either a filename or
@fieldname, we the location will be determined
from the value of the @fieldname supplied with
the search
-->
<Location><![CDATA[@SearchLocalityName]]></Location>
<Base><![CDATA[C:\\CSVs\\]]></Base>
<SearchLocalityName>
<!-- United Kingdom -->
<Name value='AA01'>AA01.txt</Name>
<Name value='United Kingdom'>AA01.txt</Name>
<Name value='UK'>AA01.txt</Name>
</SearchLocalityN