views:

663

answers:

2

So, I am trying to deploy a Web Application feature that updates the web.config using the WebConfigModifications collection. There is lots of information on doing this including all the issues you run into so I am very confident in the code but no matter what I try the config elements are not added to the web config. This works fine in the development single server environment but does not update in the Farm.

No errors, no log information no event messages .... nothing pointing toward why it will not update:

SPWebConfigModification appSettingModification = new SPWebConfigModification();
appSettingModification.Name = "add[@key=\"Key\"][@value=\"Value\"]";
appSettingModification.Path = "configuration/appSettings";
appSettingModification.Owner = "Owner";
appSettingModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
appSettingModification.Value = __appSettingResource;

if (removeModification)
{
app.WebConfigModifications.Remove(appSettingModification);
}
else
{
app.WebConfigModifications.Add(appSettingModification);
}
app.Update();
app.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

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

EDIT

I wanted to add to this based on a suggestion by drax to reflect the ApplyWebModifications method. This method is on the SPWebService that is in the Microsoft.SharePoint.Administration namespace in the Microsoft.SharePoint assembly.

One of the issues with using this method is that all web.config files are updated in the farm even if no modifications are actually being applied. This causes the app pools to be recycled even if you do not intend to update the web config. Reflection shows why this is happening and it is very annoying.

Inside the method is a short foreach loop:

foreach (SPWebApplication application in this.WebApplications)
{
application.ApplyWebConfigModifications();
}

It is looping through each of the web applications and calling an INTERNAL ApplyWebConfigModifications method on the web application objects. If they just made this method public then we would be able to call the method individually without affecting the app pool recycling on web applications that are not affected by our updates. Just sayin'

A: 

Hi, problem is propably in your call of ApplyWebCOnfigModifications() instead of:

app.Farm.Services.GetValue().ApplyWebConfigModifications();

use:

app.Farm.Servers.GetValue<SPWebService>().ApplyWebConfigModifications();

The second code actually calls service responsible for updating web.config files.

Sidenote: This code will actually open all webconfig files in all webapplications installed on server/farm :) but it will update just right ones. If you will use reflector to scan this method you will see some nice example of using foreach loop :)

drax
Actually I did have the <SPWebService> but I forgot to HTML Encode it in the question so it didn't show up. Turns out it was my issue.
webwires
A: 

This turns out to be my issue. One of the classic cases of fixing one problem but in advertantly adding a second issue I didn't detect and still thinking it was the initial problem.

webwires