Hi CS,
To use a class library which has its own configurations either in a WinForm
application or a Web application, since .NET can only load a default
configuration file per AppDomain (which is by default the .exe.config with
the executing assembly), the dll cannot have separate complete
configuration file for it. However, using an optional attribute named
"configSour ce" for the confugration file's section elements, we could put
some configuration sections in a separate config file, this will prevent
copying/merging large parts of the configuration data, only the
configSource attribute is needed to modify.
=============== =========
General Attributes Inherited by Section Elements
http://msdn2.microsoft.com/en-us/library/ms228167.aspx
configSource
Optional String attribute.
Specifies the name of the include file in which the associated
configuration section is defined, if such a file exists. Programmaticall y
accessible through the ConfigSource property.
=============== =========
Here's some usage scenario:
1) Create a Class Library named "ClassLibrary1" , use the Settings Designer
to add some application-wide settings into it.
2) Open app.config, it will roughly have following content:
<?xml version="1.0" encoding="utf-8" ?>
<configuratio n>
<configSections >
<sectionGroup name="applicati onSettings"
type="System.Co nfiguration.App licationSetting sGroup, System,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9" >
<section name="ClassLibr ary1.Properties .Settings"
type="System.Co nfiguration.Cli entSettingsSect ion, System, Version=2.0.0.0 ,
Culture=neutral , PublicKeyToken= b77a5c561934e08 9" requirePermissi on="false"
/>
</sectionGroup>
</configSections>
<applicationSet tings>
<ClassLibrary1. Properties.Sett ings>
<setting name="libPath" serializeAs="St ring">
<value>d:\tem p</value>
</setting>
</ClassLibrary1.P roperties.Setti ngs>
</applicationSett ings>
</configuration>
3) Create a new xml file, named it as "ClassLibrary1. dll.settings.co nfig",
copy the inner content of the <applicationSet tingstag to this file:
<ClassLibrary1. Properties.Sett ings>
<setting name="libPath" serializeAs="St ring">
<value>d:\tem p</value>
</setting>
</ClassLibrary1.P roperties.Setti ngs>
4) Modify app.config: remove the inner content of the tag
<ClassLibrary1. Properties.Sett ings>, add an attribute "configSour ce" to it
as:
<applicationSet tings>
<ClassLibrary1. Properties.Sett ings
configSource="C lassLibrary1.dl l.settings.conf ig">
</ClassLibrary1.P roperties.Setti ngs>
</applicationSett ings>
5) Note the Settings Designer will still work correctly, modifying settings
will reflect changes to the separate file
"ClassLibrary1. dll.settings.co nfig" instead of the App.Config.
6) For test purpose, create a static property in one public class in this
class library to return the setting "libPath".
7) Now create a winform/console/web application, reference the class
library, modify this application's app.config or web.config to modify the
<configSections part as:
<configSections >
<sectionGroup name="applicati onSettings"
type="System.Co nfiguration.App licationSetting sGroup, System,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9" >
<section name="ClassLibr ary1.Properties .Settings"
type="System.Co nfiguration.Cli entSettingsSect ion, System, Version=2.0.0.0 ,
Culture=neutral , PublicKeyToken= b77a5c561934e08 9" requirePermissi on="false"
/>
</sectionGroup>
</configSections>
<applicationSet tings>
<ClassLibrary1. Properties.Sett ings
configSource="C lassLibrary1.dl l.settings.conf ig">
</ClassLibrary1.P roperties.Setti ngs>
</applicationSett ings>
8) Copy the ClassLibrary1.d ll.settings.con fig from the class library
project to this project. If this is the web project, just copy to the same
directory with web.config; otherwise, make sure you set its "Copy to output
directory" property to "Copy always".
9) Now calls into the class library's static property defined in step 6),
change the local 'ClassLibrary1. dll.settings.co nfig', it should reflect
updated value.
Hope this helps. Let me know if there's anything unclear.
Sincerely,
Walter Wang (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.
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/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.