SetRawXML not working

W

WT

Hello,
I am trying to update a configuration section for my web.config with
SetRawXml withoutr result.
I have given modfidy rigth to Network Services account which is running the
..NET2 pool in wich my application is running, here is my code:
System.Configuration.Configuration config =
(System.Configuration.Configuration)WebConfigurationManager.OpenWebConfiguration("~");
ConfigurationSection appSettings = config.AppSettings;
if (appSettings != null)
{
if (!appSettings.SectionInformation.IsProtected)
{
if (!appSettings.ElementInformation.IsLocked)
{
appSettings.SectionInformation.SetRawXml(AppSettingsXml.Text);
}
else
System.Diagnostics.Trace.WriteLineIf(ModuleTraceSwitch.Sw.TraceVerbose,
string.Format("Can't update, section {0} is locked ",
appSettings.SectionInformation.Name));
}
else
{
System.Diagnostics.Trace.WriteLineIf(ModuleTraceSwitch.Sw.TraceVerbose,
string.Format("can't update, section {0} is protected by
{1}",appSettings.SectionInformation.Name,appSettings.SectionInformation.ProtectionProvider.Name));
}
// Save the encrypted section.
appSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);



After this no change in Web.config ?

Any idea on my error welcome.

CS
 
S

Steven Cheng[MSFT]

Hello CS,

As for the updating AppSettings in web.config file case, did you get any
exception or explicit error when you saving the configuration? Also, is
your web.config file or at least the appsettings section encrypted?

Based on my experience, for configuration file which has been encrypted,
we're suggested to use the configuration model based API (the
sectionElement or handler ) class to manipulate the appsettings. e.g.

===========
AppSettingsSection appset = config.GetSection("appSettings") as
AppSettingsSection;

if (appset != null)
{
appset.Settings.Add(new KeyValueConfigurationElement("newkey" +
DateTime.Now.Ticks, "new value"));

}
=============

Is there any particular concern that you can not use the typed API in your
case? Also, for the SetRawXML method, I've also peformed some test on my
side, the following code which use "SectionInformation.SetRawXml" method
works correctly on my side(on a XP SP2 box, in both VS 2005 test server and
IIS server). You can test it and let me know the result you get.

=================================
protected void btnSet_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

AppSettingsSection appset = config.GetSection("appSettings") as
AppSettingsSection;

if (appset != null)
{
appset.Settings.Add(new KeyValueConfigurationElement("newkey" +
DateTime.Now.Ticks, "new value"));
}

string rawXML =
"<appSettings> <add key=\"key1\" value=\"value1\" /><add
key=\"key2\" value=\"value2\" /><add key=\"key3\" value=\"value3\"
/></appSettings>";

appset.SectionInformation.SetRawXml(rawXML);

config.Save();
}
=================================

Please feel free to let me know if you have any further finding.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



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

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

WT

Hello Steven,

Your sample doesn't work on a W2K3 server, specially
string rawXML =
"<appSettings> <add key=\"key1\" value=\"value1\" /><add
key=\"key2\" value=\"value2\" /><add key=\"key3\" value=\"value3\"
/></appSettings>";

appset.SectionInformation.SetRawXml(rawXML);

config.Save();

There are no exceptions but web.config is nor changed. Even with all rights
to modify set on web.config.

CS.
 
W

WT

Sorry Steven,
I realized that my exception trapping was not perfect, I get the following
ex and don't understand what is this str parameter from the exception text ?

ex: System.Configuration.ConfigurationErrorsException: An error occurred
executing the configuration section handler for appSettings. --->
System.ArgumentNullException: Value cannot be null.
Parameter name: str
at
System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[]
str)
at
System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess
access, AccessControlActions control, String[] pathListOrig, Boolean
checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at
System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess
access, String path)
at System.Configuration.PropertySourceInfo.get_FileName()
at System.Configuration.ElementInformation.get_Source()
at System.Configuration.AppSettingsSection.DeserializeElement(XmlReader
reader, Boolean serializeCollectionKey)
at System.Configuration.ConfigurationSection.DeserializeSection(XmlReader
reader)
at
System.Configuration.MgmtConfigurationRecord.SetRawXml(ConfigurationSection
configSection, String xmlElement)
--- End of inner exception stack trace ---
at
System.Configuration.MgmtConfigurationRecord.SetRawXml(ConfigurationSection
configSection, String xmlElement)
at System.Configuration.SectionInformation.SetRawXml(String rawXml)
at
AddonNice.WTPageModules.EncryptConfig.EncryptConfig.UpdateButton_OnClick(Object
source, EventArgs e)



Any idea ?

Thanks
CS
 
S

Steven Cheng[MSFT]

Thanks for your followup CS,

I'll perform further test on a windows 2k3 server box to see the result.
I'll update you as soon as possible.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

WT

Error seems to be caused by the file attribute
<appSettings file="MyFile.config">

when removed everything works Ok ???

but this attribut is working normally for the pgm ?????

CS



WT said:
Sorry Steven,
I realized that my exception trapping was not perfect, I get the following
ex and don't understand what is this str parameter from the exception text
?

ex: System.Configuration.ConfigurationErrorsException: An error occurred
executing the configuration section handler for appSettings. --->
System.ArgumentNullException: Value cannot be null.
Parameter name: str
at
System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[]
str)
at
System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess
access, AccessControlActions control, String[] pathListOrig, Boolean
checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at
System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess
access, String path)
at System.Configuration.PropertySourceInfo.get_FileName()
at System.Configuration.ElementInformation.get_Source()
at System.Configuration.AppSettingsSection.DeserializeElement(XmlReader
reader, Boolean serializeCollectionKey)
at
System.Configuration.ConfigurationSection.DeserializeSection(XmlReader
reader)
at
System.Configuration.MgmtConfigurationRecord.SetRawXml(ConfigurationSection
configSection, String xmlElement)
--- End of inner exception stack trace ---
at
System.Configuration.MgmtConfigurationRecord.SetRawXml(ConfigurationSection
configSection, String xmlElement)
at System.Configuration.SectionInformation.SetRawXml(String rawXml)
at
AddonNice.WTPageModules.EncryptConfig.EncryptConfig.UpdateButton_OnClick(Object
source, EventArgs e)



Any idea ?

Thanks
CS







Steven Cheng said:
Hello CS,

As for the updating AppSettings in web.config file case, did you get any
exception or explicit error when you saving the configuration? Also, is
your web.config file or at least the appsettings section encrypted?

Based on my experience, for configuration file which has been encrypted,
we're suggested to use the configuration model based API (the
sectionElement or handler ) class to manipulate the appsettings. e.g.

===========
AppSettingsSection appset = config.GetSection("appSettings") as
AppSettingsSection;

if (appset != null)
{
appset.Settings.Add(new KeyValueConfigurationElement("newkey"
+
DateTime.Now.Ticks, "new value"));

}
=============

Is there any particular concern that you can not use the typed API in
your
case? Also, for the SetRawXML method, I've also peformed some test on my
side, the following code which use "SectionInformation.SetRawXml" method
works correctly on my side(on a XP SP2 box, in both VS 2005 test server
and
IIS server). You can test it and let me know the result you get.

=================================
protected void btnSet_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

AppSettingsSection appset = config.GetSection("appSettings") as
AppSettingsSection;

if (appset != null)
{
appset.Settings.Add(new KeyValueConfigurationElement("newkey"
+
DateTime.Now.Ticks, "new value"));
}

string rawXML =
"<appSettings> <add key=\"key1\" value=\"value1\" /><add
key=\"key2\" value=\"value2\" /><add key=\"key3\" value=\"value3\"
/></appSettings>";

appset.SectionInformation.SetRawXml(rawXML);

config.Save();
}
=================================

Please feel free to let me know if you have any further finding.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



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

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

Steven Cheng[MSFT]

Hello CS,

Thanks for your reply and the further information.

I've also performed some further test on my local working environment. Here
is the further info I've got:

1. The code works identically on windows XP and windows server 2003. I've
tried the same application code (that modify the web.config file by
SetRawXml ) and as long as the worker process identtiy has sufficient NTFS
file access permission, and application is running under "Full" trust
level(ASP.NET trust level), it can work correctly.

2. As for the <appSettings file="MyFile.config"> case, this does be a
problem here. the "file" attribute is .net 1.x feature and it is specific
to <appSettings> to separate the section into an external file. However,
in .net framework 2.0, it has provide a new "configSource" setting that can
be applied to all configuration sections.

#appSettings Element (General Settings Schema)
http://msdn2.microsoft.com/en-us/library/ms228154.aspx

here is the description picked up from the above reference:

<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>
Note In the .NET Framework version 2.0, you can now include configuration
settings in a separate file for all configuration elements that support the
configSource attribute. However, when you use the configSource attribute,
you must move the entire section to the separate file because there is no
merging of element settings. There is a one-time write to the Web.config
file when using the configSource attribute. This causes the application to
restart, but subsequent updates to the section are written directly to the
separate file and do not cause subsequent application restarts. For more
information, see ConfigSource.
<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>

So in your web.config, you can change to use the "configSource" attribute
to specify a separate source. e.g.

======in web.config==========
<configuration>
<appSettings configSource="AppSettings.xml">
.............
==========

====in AppSettings.xml==========
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
</appSettings>
===========

thus, the SetRawXml code works correctly(the external AppSettings.xml file
is updated).

test code is quite straight as below:

=========code===============
protected void btnSet_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

AppSettingsSection appset = config.GetSection("appSettings") as
AppSettingsSection;

string rawXML =
"<appSettings> <add key=\"key1\" value=\"value1\" /><add
key=\"key2\" value=\"value2\" /><add key=\"key3\" value=\"value3\"
/></appSettings>";

appset.SectionInformation.SetRawXml(rawXML);

config.Save();
}
=============================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

WT

Great, very complete.
CS
Steven Cheng said:
Hello CS,

Thanks for your reply and the further information.

I've also performed some further test on my local working environment.
Here
is the further info I've got:

1. The code works identically on windows XP and windows server 2003. I've
tried the same application code (that modify the web.config file by
SetRawXml ) and as long as the worker process identtiy has sufficient NTFS
file access permission, and application is running under "Full" trust
level(ASP.NET trust level), it can work correctly.

2. As for the <appSettings file="MyFile.config"> case, this does be a
problem here. the "file" attribute is .net 1.x feature and it is specific
to <appSettings> to separate the section into an external file. However,
in .net framework 2.0, it has provide a new "configSource" setting that
can
be applied to all configuration sections.

#appSettings Element (General Settings Schema)
http://msdn2.microsoft.com/en-us/library/ms228154.aspx

here is the description picked up from the above reference:

<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>
Note In the .NET Framework version 2.0, you can now include
configuration
settings in a separate file for all configuration elements that support
the
configSource attribute. However, when you use the configSource attribute,
you must move the entire section to the separate file because there is no
merging of element settings. There is a one-time write to the Web.config
file when using the configSource attribute. This causes the application to
restart, but subsequent updates to the section are written directly to the
separate file and do not cause subsequent application restarts. For more
information, see ConfigSource.
<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>

So in your web.config, you can change to use the "configSource" attribute
to specify a separate source. e.g.

======in web.config==========
<configuration>
<appSettings configSource="AppSettings.xml">
............
==========

====in AppSettings.xml==========
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
</appSettings>
===========

thus, the SetRawXml code works correctly(the external AppSettings.xml file
is updated).

test code is quite straight as below:

=========code===============
protected void btnSet_Click(object sender, EventArgs e)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

AppSettingsSection appset = config.GetSection("appSettings") as
AppSettingsSection;

string rawXML =
"<appSettings> <add key=\"key1\" value=\"value1\" /><add
key=\"key2\" value=\"value2\" /><add key=\"key3\" value=\"value3\"
/></appSettings>";

appset.SectionInformation.SetRawXml(rawXML);

config.Save();
}
=============================

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

Steven Cheng[MSFT]

You're welcome. :)

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 

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