Share AppSettings configuration between a service Web and an asp.net web application

O

Oriane

Hi there,
In a web service (A), I would like to read and modify a web site (B)
configuration file. My goal is only to modify the appSettings section.

In order to avoid mistakes, I would like not to have these settings in the
default web.config file but, say, in a "settings.config" file, located in
the webservice virtual directory.

On IIS6, my webservice A is a web application from the B web site. So, the A
source files are in a (virtual) subdirectory of the B source files
directory.

Does it seem possible ?
___________________________________________________________________________________________________
Now, for a start, I've tried to read the settings in the web.config file of
A:

<?xml version="1.0"?>
<Configuration>
<...>
<appSettings>
<add key="a" value="aaa"/>
</appSettings>
<connectionStrings>
<clear/>
<add name="Stibil" connectionString="Server=.\sql2008..."/>
</connectionStrings>

I use this code:

System.Configuration.Configuration rootWebConfig1 =
WebConfigurationManager.OpenWebConfiguration(null);
if (rootWebConfig1.AppSettings.Settings.Count > 0)
{
KeyValueConfigurationElement customSetting =
rootWebConfig1.AppSettings.Settings[param];
if (customSetting != null)
return customSetting.Value;
}
return "Nothing";

And the fact is that "rootWebConfig1.AppSettings.Settings.Count" returns
0...
However,
"rootWebConfig1.AppSettings.ConnectionStrings..ConnectionStrings.ConnectionStrings.Count"
is 1.

I can't see where is my mistake...

__________________________________________________________________________________________________
Now if I want to read a config file (in my web service or in my web site),
which is not the default "web.config" file, possibly located in a virtual
subdirectory, what parameter do I need in the "OpenWebConfiguration" method
?

Best regards
 
S

Steven Cheng

Hi Oriane,

From your description, you want to use code to open the Web.config file of
the IIS root site and modify the AppSettings section in it, correct?

Based on my research, the WebConfigurationManager class can only open
"web.config" file, so if you use your own custom config file, it will not
work with it. And to open the web.config file for the IIS root site, you
only need to supply the root site virutal path which is simply "/"

Here is a test function which open & modify web.config file in IIS site
root(from an ASP.NET application/virtual directory under the site root):

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

public partial class ConfigTestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnAddEntry_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration("/");
config.AppSettings.Settings.Add("Item_" + DateTime.Now.Ticks,
"Value_" + DateTime.Now.Ticks);
Response.Write("<br/>Physical path: " + config.FilePath);
config.Save(ConfigurationSaveMode.Modified);
}
}
=================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 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. 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/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
O

Oriane

Hi Steven,

"Steven Cheng" said:
Hi Oriane,

From your description, you want to use code to open the Web.config file of
the IIS root site and modify the AppSettings section in it, correct?
Yes. Correct !

Based on my research, the WebConfigurationManager class can only open
"web.config" file, so if you use your own custom config file, it will not
work with it. And to open the web.config file for the IIS root site, you
only need to supply the root site virutal path which is simply "/"

Here is a test function which open & modify web.config file in IIS site
root(from an ASP.NET application/virtual directory under the site root):
Ok thanks a lot.

Oriane
 
S

Steven Cheng

Thanks for your reply Oriane,

If you have any further question on this, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).


--------------------
 

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

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top