Configuration Model in .Net 2.0

H

Hardy Wang

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?
 
L

Linda Liu [MSFT]

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/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.
 
G

Guest

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.
 
L

Linda Liu [MSFT]

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
 
L

Linda Liu [MSFT]

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
 
G

GiddyUpHorsey

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.
 

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