customer configuration

G

Guest

My Windows Application stores its configuration values in app.config and
these are typed and accessed via corresponding classes, something like this...

<configSections>
<section name="Watcher"/>
</configSections>

<Watcher>
<ClockWatcherCollection>
<add Name="Blah"/>
</ClockWatcherCollection>
</Watcher>

Friend Class Watcher

Inherits System.Configuration.ConfigurationSection

<System.Configuration.ConfigurationProperty("ClockWatcherCollection",
IsRequired:=True)> _
Friend ReadOnly Property ClockWatcherCollection() As
ClockWatchCollection
Get
Return Me("ClockWatcherCollection")
End Get
End Property

End Class

Friend Class ClockWatchCollection

Inherits System.Configuration.ConfigurationElementCollection

Protected Overloads Overrides Function CreateNewElement() As
System.Configuration.ConfigurationElement
Return New ClockWatch
End Function

Protected Overrides Function GetElementKey(ByVal Element As
System.Configuration.ConfigurationElement) As Object
Return CType(Element, ClockWatch).Name
End Function

Friend Shadows Function Item(ByVal Name As String) As ClockWatch
Return MyBase.BaseGet(Name)
End Function

End Class

Friend Class ClockWatch

Inherits System.Configuration.ConfigurationSection

<System.Configuration.ConfigurationProperty("Name", IsRequired:=True)> _
Friend ReadOnly Property Name() As String
Get
Return Me("Name")
End Get
End Property

End Class

This works fine for simple types, such as the Name property above, but I
can’t see how to do the same for complex types like the collection of
Notification types and the LogFile type below…

<Watcher>
<ClockWatcherCollection>
<add Name="Blah">
<Notification Address="Blah"/>
<Notification Address="Blah"/>
<Notification Address="Blah"/>
<LogFile Name="Blah", MaxSize="Blah", Delete="Blah"/>
</add>
</ClockWatcherCollection>
</Watcher>
 
S

Steven Cheng[MSFT]

Hi Dick,

From your description, you're wantting to add some custom configuration
section to the applicaiton config file, and some of the configure element
contains item collections, and you met some problem to get complex type
collection in the custom configsection, correct?

Based on my understanding, the .net framework 2.0 custom configuration
interface support two kinds of custom configuration handler:

1. derive your custom configuration section class from
"ConfigurationSection" class, and any sub property/section from
"ConfigurationElement" or "ConfigurationElementCollection". And if you
want to add custom item collection this way, you can only contains a single
type of element in one colleciton and use "<add>", "<remove>" element to
add or remove item. e.g.

==================
<urls>
<clear />
<add
name="Microsoft" url="http://www.microsoft.com" port="0"
lockAllAttributesExcept="port" />
<add
name="Contoso" url="http://www.contoso.com/" port="8080"
lockAllAttributesExcept="port" lockItem="true" />
</urls>
========================

and it only support simple/basic types rather than nested complex
properties.


2. For your scenario, if you want to add complex nested properties for each
item in configuration colleciton/list, I'm afraid you may need to write a
custom section handler and use raw XML API or XML serialization api to
manage the XML content of the section directly. see the following MSDN
reference:

#How to: Create Custom Configuration Sections Using
IConfigurationSectionHandler
http://msdn2.microsoft.com/en-us/library/ms228056.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top