Using ConfigurationManager & custom configuration sections |
|
Author |
Message |
J B Li

|
Posted: .NET Base Class Library, Using ConfigurationManager & custom configuration sections |
Top |
Hi there,
I'm trying to use the ConfigurationSection class from the ConfigurationManager because I need to store some repeated custom configuration settings that have multiple properties. Using C# Express Edition 2005 Beta 2, I've built the example below based on an example I downloaded from the Internet. It compiles OK but when I run it I get the following error message: An error occurred creating the configuration section handler for systems: Could not load type 'SystemsSection' from assembly 'ConfigConsoleApplication'. (C:\Documents and Settings\Jean\Local Settings\Application Data\Temporary Projects\ConfigConsoleApplication\bin\Debug\ConfigConsoleApplication.EXE.config line 4) I newbie to C#, I've been head-butting the screen all day yesterday - I suspect there is something blindingly obvious that I've missed but I don't know what 
Thanks & regards,
J B Li London |
|
|
|
 |
David Banister

|
Posted: .NET Base Class Library, Using ConfigurationManager & custom configuration sections |
Top |
I believe that the type name must also include the namespace. Try using type="ConfigConsoleApplication.SystemsSection,ConfigConsoleApplication" instead.
|
|
|
|
 |
J B Li

|
Posted: .NET Base Class Library, Using ConfigurationManager & custom configuration sections |
Top |
Hi David,
[CLAPS HAND ON FOREHEAD] Thanks for that - someone else also e-mailed me with the same solution ! And of course it works 
Here's the solution again with your correction in place for the benefit of anyone else looking for a small working example. I removed the redundant "default" property from my original posting and added code to output the configuration data.
Thanks again & regards,
Jean (<-- it's a French name).
|
|
|
|
 |
Jim_Martin

|
Posted: .NET Base Class Library, Using ConfigurationManager & custom configuration sections |
Top |
Hi,
I am trying to work through the example you provided, and I cannot get it to compile. I keep getting the following exception:
Error 18 'ConfigConsoleApplication.SystemsCollection.CollectionType': cannot change access modifiers when overriding 'public' inherited member 'System.Configuration.ConfigurationElementCollection.CollectionType' C:\TheCloset\CustomConfigSample\CustomConfigSample\Program.cs 39 57 CustomConfigSample
Do you have any idea what would be causing this I have included the reference to the System.configuration dll, and have made no other changes to the code.
Thanks,
Jim Martin.
|
|
|
|
 |
Jim_Martin

|
Posted: .NET Base Class Library, Using ConfigurationManager & custom configuration sections |
Top |
Looks like the issue is due to using the RTM version of Visual Studio. MS changes a few items.
Firts, I needed to change the
protected override ConfigurationElementCollectionType CollectionType to
public override ConfigurationElementCollectionType CollectionType After this, several of the attribute parameter names were changed in the final release. I am including the code as I was able to get it compiling.
Thanks to all who contributed to this. It has been very helpful to me!
Jim.
------------------------------------------------------- App.Config
| | < xml version="1.0" encoding="utf-8" > < configuration> < configSections> < section name="systems" type="ConfigConsoleApplication.SystemsSection, ConfigConsoleApplication" /> </ configSections> < systems> < system name="Production" server="PRODSERVER" database="Prod" /> < system name="Demo" server="DEMOSERVER" database="Demo" /> < system name="Testing" server="TESTSERVER" database="Test" /> < system name="Development" server="DEVSERVER" database="DEV" /> </ systems> </ configuration>
|
----------------------------------------------------- Program.cs
| | using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Configuration; // remember to add reference in project using System.Reflection; using System.Text; namespace ConfigConsoleApplication { public sealed class SystemsSection : ConfigurationSection { public SystemsSection() { } [ ConfigurationProperty( "", IsDefaultCollection = true )] public SystemsCollection Systems { get { return (SystemsCollection)base[""]; } } } public sealed class SystemsCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new SystemElement(); } protected override object GetElementKey( ConfigurationElement element ) { return ((SystemElement)element).Name; } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } } protected override string ElementName { get { return "system"; } } } public sealed class SystemElement : ConfigurationElement { [ ConfigurationProperty( "name", IsKey = true, IsRequired = true )] public string Name { get { return (string)base["name"]; } set { base["name"] = value; } } [ ConfigurationProperty( "server", IsRequired = true )] public string Server { get { return (string)base["server"]; } set { base["server"] = value; } } [ ConfigurationProperty( "database", IsRequired = true )] public string Database { get { return (string)base["database"]; } set { base["database"] = value; } } public override string ToString() { string output = "SystemElement :\n"; output += string.Format( "Name = {0}\n", Name ); output += string.Format( "Server = {0}\n", Server ); output += string.Format( "Database = {0}\n", Database ); return output; } } public class Program { static void Main( string[] args ) { SystemsSection sysSection = ConfigurationManager.GetSection( "systems" ) as SystemsSection; SystemsCollection oneCollection = sysSection.Systems; foreach( SystemElement oneElement in oneCollection ) { Console.WriteLine( oneElement.ToString() ); } } } } |
|
|
|
|
 |
Regula

|
Posted: .NET Base Class Library, Using ConfigurationManager & custom configuration sections |
Top |
Hi,
I can read the following data.
<MyConfig> <Emp1 Run="True"> <Test Name="Line1" Run="True"> <x Name="Station1" Run="True"></x> <x Name="Station2" Run="True"></x> </Test> <Test Name="Line2" Run="True"> <x Name="Station3" Run="True"></x> <x Name="Station4" Run="True"></x> </Test> </Emp1> </MyConfig>
Regs
Satya
|
|
|
|
 |
April Adams

|
Posted: .NET Base Class Library, Using ConfigurationManager & custom configuration sections |
Top |
Thanks for the thread, you guys helped me create a custom config section for my VB.NET project, so I thought I would post the translated code in case anyone needs it
Imports system
Imports System.configuration
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports System.Reflection
Imports System.text
Namespace ConfigConsoleApplication
Public Class SystemsSection
Inherits ConfigurationSection
Public Sub PrintSettings()
End Sub
<ConfigurationProperty( "", IsDefaultCollection:=True)> _
Public ReadOnly Property Systems() As SystemsCollection
Get
Return CType(Me(""), SystemsCollection)
End Get
End Property
End Class
Public Class SystemsCollection
Inherits ConfigurationElementCollection
Protected Overloads Overrides Function CreateNewElement() As System.Configuration.ConfigurationElement
Return New SystemElement()
End Function
Protected Overrides Function GetElementKey(ByVal element As System.Configuration.ConfigurationElement) As Object
Return CType(element, SystemElement).Name
End Function
Public Overrides ReadOnly Property CollectionType() As System.Configuration.ConfigurationElementCollectionType
Get
Return ConfigurationElementCollectionType.BasicMap
End Get
End Property
Protected Overrides ReadOnly Property ElementName() As String
Get
Return "system"
End Get
End Property
End Class
Public Class SystemElement
Inherits ConfigurationElement
<ConfigurationProperty( "name", IsKey:=True, isRequired:=True)> _
Public Property Name() As String
Get
Return Me("name")
End Get
Set(ByVal value As String)
Me("name") = value
End Set
End Property
<ConfigurationProperty( "server", isRequired:=True)> _
Public Property Server() As String
Get
Return Me("server")
End Get
Set(ByVal value As String)
Me("server") = value
End Set
End Property
<ConfigurationProperty( "database", isRequired:=True)> _
Public Property Database() As String
Get
Return Me("database")
End Get
Set(ByVal value As String)
Me("database") = value
End Set
End Property
Public Overrides Function ToString() As String
Dim output As String
output = "SystemElement :\n"
output += String.Format("Name = {0}\n", Name)
output += String.Format("Server = {0}\n", Server)
output += String.Format("Database = {0}\n", Database)
Return output
End Function
End Class
End Namespace
|
|
|
|
 |
Eric G Wang

|
Posted: .NET Base Class Library, Using ConfigurationManager & custom configuration sections |
Top |
hi, Jim Martin & Jean
Your codes are really cool! They has been very helpful to me! Now i wanna do something a little different. i wish i can use a customized configuration file "myapp.config" , whose content is same as the file "App.config".
myapp.config:
< xml version="1.0" encoding="utf-8" > <configuration> <configSections> <section name="systems" type="ConfigConsoleApplication.SystemsSection, ConfigConsoleApplication" /> </configSections> <systems> <system name="Production" server="PRODSERVER" database="Prod" /> <system name="Demo" server="DEMOSERVER" database="Demo" /> <system name="Testing" server="TESTSERVER" database="Test" /> <system name="Development" server="DEVSERVER" database="DEV" /> </systems> </configuration>
MySampleCode scriptlet:
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); SystemsSection sysSection = cfg.GetSection("systems") as SystemsSection; ......
Why the variant "sysSection" is null Do you have any idea about it Thank you guys in advance!!!
Eric G Wang
|
|
|
|
 |
Eric G Wang

|
Posted: .NET Base Class Library, Using ConfigurationManager & custom configuration sections |
Top |
hi Satya,
Can you show me some Sample Codes Thanks a lot!!!
Eric G Wang
|
|
|
|
 |
|
|