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

Configuration Model in .Net 2.0

P: n/a
Hi all,
I understand, in .Net 2.0 we have little bit different model to read
config file from .Net 1.1. Basically I need to use
WebConfigurationManager.AppSettings to read from web.config, and
ConfigurationManager.AppSettings to read from Windows Application config.
Here is my question, I have a class library project which will be
referenced by both Web and Windows Application in different projects, I need
to read config setting inside this class library, how should I know which
environment is it in (web or windows) and based on environment to call
different API to load setting? And what is the best practice in this
scenario to handle config file?

--
WWW: http://www.imagestation.com/members/hardywang
ICQ: 3359839
yours Hardy
Sep 29 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

I am sorry to say that I don't think you should read or write app.config or
web.config file in a class library. Configuration file doesn't make sense
for a class library.

Suppose we have a class library project and a Windows application project.
We add a configuration file and access it within the class library project
and build the class library project. We add a reference to the class
library project in the Windows application project and then build the
Windows application project. You will see that the configuration file of
the class library project isn't copied to the 'bin\debug' directory of the
Windows application project.

The class library won't read value of the settings from its configuration
file, instead it will adopt the default value. Even if we copy the
configuration file of the class library to the 'bin\debug' directory of the
Windows application project and modify the value of the settings in the
configuration file, the class library won't see the new value of the
settings.

So we should add and access application settings in the Windows application
or Web application project.

Hope this helps.

If you have any concerns, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 2 '06 #2

P: n/a
Hi Linda,
Thanks for your reply, I think I did not make myself clear.

What I am talking is, class library reads configuration from either
web.config or Application.exe.config. For example, there is a key named
"Email" in web.config for my web application, in Application.exe.config for
my Application.exe windows form application. The class library doesn't
include any config file with it, it just reads this key in both environments.

"Linda Liu [MSFT]" wrote:
Hi,

I am sorry to say that I don't think you should read or write app.config or
web.config file in a class library. Configuration file doesn't make sense
for a class library.

Suppose we have a class library project and a Windows application project.
We add a configuration file and access it within the class library project
and build the class library project. We add a reference to the class
library project in the Windows application project and then build the
Windows application project. You will see that the configuration file of
the class library project isn't copied to the 'bin\debug' directory of the
Windows application project.

The class library won't read value of the settings from its configuration
file, instead it will adopt the default value. Even if we copy the
configuration file of the class library to the 'bin\debug' directory of the
Windows application project and modify the value of the settings in the
configuration file, the class library won't see the new value of the
settings.

So we should add and access application settings in the Windows application
or Web application project.

Hope this helps.

If you have any concerns, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Oct 5 '06 #3

P: n/a
Hi Hardy,

Thank you for your update and the clarification of the problem.

Firstly, I don't think we could read&write the configuration file of
project A in project B using AppSettings. But if we treat the configuration
file as a normal XML file in project B(I mean use XML DOM to access the
file), we can access the configuration file of project A in project B.

Secondly, since the configuration is used by the class library, I recommend
you to define the configuration file by yourself (don't use the
configuration file that is generated automatically by VS when we add
application settings in the Windows or Web projects). Thus, no matter which
type of the project the class library is refereced to, the class library
will access the same configuration file.

Hope this helps.
If you have anything unclear, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

Oct 6 '06 #4

P: n/a
Hi Hardy,

How about your solving the problem now?

I have done more research on this problem and found that I made a mistake
in my previous reply. I was focusing on reading the configuration file with
the strongly typed settings class generated by the
SettingsSingleFileGenerator custom tool, which is associated with settings
files by default. I meant it's no possible to read the configuration file
of the Windows application or Web application from the class library using
the strong typed settings class.

If we use ConfigurationManager class, we could access any configuration
file as long as we specify the filename of the configuration file.

As for the question how to know which environment is the class library in,
I don't think there's a easy way to do that. But I think there's a simple
way. Suppose there's a function in the class library to read configuration
files. We could add a parameter in the function to specify which type of
the config file is. When we call the function from the Windows application
or Web application, pass a value to the parameter to specify the type of
the configuration file.

Hope this helps.
If you have any concerns, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Oct 12 '06 #5

P: n/a
I too need this functionality. My class library needs to be able to
read some configuration settings and have it automatically come from
the app.config or web.config depending on whether the application using
the class library is a Windows or ASP.NET application.

Nov 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.