By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,245 Members | 2,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,245 IT Pros & Developers. It's quick & easy.

Getting settings values in an assermbly library from an ASP.NET or Winform

P: n/a
WT
Hello,

Using VS2005.
I have an assembly library that can be called from a Web site asp.net
application or from a winform application.
From this library I need to retrieve a path using simply a key like
'libPath'.
As far as winform and asp.net share the same common base class for settings,
SettingsBase, how to manage this ?
For winform the value should be set in app.config and for web site in
web.config.
I am looking for some sample ?

Any help welcome.

CS
Jan 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,

Web:

- Application Settings (settings must be added to the web.config manually)
http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx
- 2.0 Provider framework
- Proprietary file
- Database
- Registry
- WebConfigurationManager
http://msdn2.microsoft.com/en-us/lib...onmanager.aspx

Non-web:

- Application Settings (settings must be added to the app.config manually)
[same link as for web]
- 2.0 Provider framework
- Proprietary file
- Database
- Registry
- ConfigurationManager
http://msdn2.microsoft.com/en-us/lib...onmanager.aspx

I'm sure there's more choices that I haven't thought of so this is probably
not an exhaustive list.

Check if HttpContext.Current and if it's not null then it's a web
application. Otherwise, it's not a web application. With this information
you may want to change how and where the settings are persisted for your
class library, especially if security is a concern.

From the information you have provided it sounds like Application Settings
may be your best bet since it's easy to use and should work for both
applications, and it seems that you're trying to go that route already.
Copy the settings created by the default settings in your class library
project from the dll's configuration file into the app.config or the
web.config and they should work. Make sure you choose settings names that
won't conflict with other settings in the actual application.

--
Dave Sexton
http://davesexton.com/blog

"WT" <WT@newsgroups.nospamwrote in message
news:u0**************@TK2MSFTNGP02.phx.gbl...
Hello,

Using VS2005.
I have an assembly library that can be called from a Web site asp.net
application or from a winform application.
From this library I need to retrieve a path using simply a key like
'libPath'.
As far as winform and asp.net share the same common base class for
settings, SettingsBase, how to manage this ?
For winform the value should be set in app.config and for web site in
web.config.
I am looking for some sample ?

Any help welcome.

CS

Jan 23 '07 #2

P: n/a
WT
Thanks,

MS has to do a last step to avoid us testing HttpContext.Current... :)
CS
"Dave Sexton" <dave@jwa[remove.this]online.coma écrit dans le message de
news: OS**************@TK2MSFTNGP06.phx.gbl...
Hi,

Web:

- Application Settings (settings must be added to the web.config manually)
http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx
- 2.0 Provider framework
- Proprietary file
- Database
- Registry
- WebConfigurationManager
http://msdn2.microsoft.com/en-us/lib...onmanager.aspx

Non-web:

- Application Settings (settings must be added to the app.config manually)
[same link as for web]
- 2.0 Provider framework
- Proprietary file
- Database
- Registry
- ConfigurationManager
http://msdn2.microsoft.com/en-us/lib...onmanager.aspx

I'm sure there's more choices that I haven't thought of so this is
probably not an exhaustive list.

Check if HttpContext.Current and if it's not null then it's a web
application. Otherwise, it's not a web application. With this
information you may want to change how and where the settings are
persisted for your class library, especially if security is a concern.

From the information you have provided it sounds like Application Settings
may be your best bet since it's easy to use and should work for both
applications, and it seems that you're trying to go that route already.
Copy the settings created by the default settings in your class library
project from the dll's configuration file into the app.config or the
web.config and they should work. Make sure you choose settings names that
won't conflict with other settings in the actual application.

--
Dave Sexton
http://davesexton.com/blog

"WT" <WT@newsgroups.nospamwrote in message
news:u0**************@TK2MSFTNGP02.phx.gbl...
>Hello,

Using VS2005.
I have an assembly library that can be called from a Web site asp.net
application or from a winform application.
From this library I need to retrieve a path using simply a key like
'libPath'.
As far as winform and asp.net share the same common base class for
settings, SettingsBase, how to manage this ?
For winform the value should be set in app.config and for web site in
web.config.
I am looking for some sample ?

Any help welcome.

CS


Jan 23 '07 #3

P: n/a
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
"configSource" 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. Programmatically
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" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGrou p, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ClassLibrary1.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ClassLibrary1.Properties.Settings>
<setting name="libPath" serializeAs="String">
<value>d:\temp</value>
</setting>
</ClassLibrary1.Properties.Settings>
</applicationSettings>
</configuration>
3) Create a new xml file, named it as "ClassLibrary1.dll.settings.config",
copy the inner content of the <applicationSettingstag to this file:

<ClassLibrary1.Properties.Settings>
<setting name="libPath" serializeAs="String">
<value>d:\temp</value>
</setting>
</ClassLibrary1.Properties.Settings>
4) Modify app.config: remove the inner content of the tag
<ClassLibrary1.Properties.Settings>, add an attribute "configSource" to it
as:

<applicationSettings>
<ClassLibrary1.Properties.Settings
configSource="ClassLibrary1.dll.settings.config">
</ClassLibrary1.Properties.Settings>
</applicationSettings>

5) Note the Settings Designer will still work correctly, modifying settings
will reflect changes to the separate file
"ClassLibrary1.dll.settings.config" 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
<configSectionspart as:

<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGrou p, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ClassLibrary1.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ClassLibrary1.Properties.Settings
configSource="ClassLibrary1.dll.settings.config">
</ClassLibrary1.Properties.Settings>
</applicationSettings>

8) Copy the ClassLibrary1.dll.settings.config 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.config', 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.

Jan 24 '07 #4

P: n/a
Please note I haven't fully tested this approach, though it seems working
on my side using a simple web site and a simple console application. If you
do find any issues, please feel free to let me know.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

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

Jan 24 '07 #5

P: n/a
WT
I will try it, seems good solution.
More later, thanks.
CS
"Walter Wang [MSFT]" <wa****@online.microsoft.coma écrit dans le message
de news: 0a**************@TK2MSFTNGHUB02.phx.gbl...
Please note I haven't fully tested this approach, though it seems working
on my side using a simple web site and a simple console application. If
you
do find any issues, please feel free to let me know.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

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

Jan 25 '07 #6

P: n/a
For me this fails to work since it only picks up default values and
not stuff from the config file.

Regards
Lars Schouw

Mar 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.