SPWebConfigModification望ましくない順序で子ノードを追加します
-
10-12-2019 - |
質問
SPWebConfig修正を使用してSystem.WebServer / Modulesセクションに2つのノードを追加するコードがあります。私がやりたいことは、<remove name="MyModule" />
を追加してから<add name="MyModule" type="[type full assembly path]">
を追加することです。
私が何をしているように思われても、web.configは常に<add ../>
の前に<remove ../>
に終わります。モジュールが追加された後に削除されるため、これは明らかに間違っています。私がやりたいことはそれを削除してからそれを追加します。これがweb.configが次のように見えます。
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="MyModule" type="[type full assembly path]" />
<remove name="MyModule" />
</modules>
</system.webServer>
</configuration>
.
これは私がそれが好きなものです:
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="MyModule" />
<add name="MyModule" type="[type full assembly path]" />
</modules>
</system.webServer>
</configuration>
.
..そしてここではspfeatureceiver.FeatureInstalledの間に実行されるコードです:
SPSecurity.RunWithElevatedPrivileges(delegate
{
SPWebService spWebService = SPWebService.ContentService;
var moduleModification = new SPWebConfigModification
{
Path = "configuration/system.webServer/modules",
Name = "remove[@name='MyModule']",
Sequence = 0,
Owner = "Sample",
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Value = "<remove name='MyModule' />",
};
spWebService.WebConfigModifications.Add(moduleModification);
spWebService.Update();
spWebService.ApplyWebConfigModifications();
moduleModification = new SPWebConfigModification
{
Path = "configuration/system.webServer/modules",
Name = "add[@name='MyModule'][@type='[type full assembly path]']",
Sequence = 1000,
Owner = "Sample",
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Value = "<add name='MyModule' type='type full assembly path' />",
};
spWebService.WebConfigModifications.Add(moduleModification);
spWebService.Update();
spWebService.ApplyWebConfigModifications();
});
.
...私はアップデートと適用ウェブコンデションを呼び出す前にこれらの変更をどちらかの変更を加えることができることを知っていますが、ここでこのようにこのコードを持っているだけでこれは上記の追加を把握できないためです。削除する。なぜそれはこのようにしているのですか?ノードをアルファベットすることを試みますか?注文を指定できますか?
解決
Yes, the nodes are being alphabetically sorted and the Sequence
property only applies when the Name
property is exactly the same. To fix, use a little XPath trickery to get your nodes to sort in the correct order (first modules[1=1]
then modules[2=2]
):
SPSecurity.RunWithElevatedPrivileges(delegate
{
SPWebService spWebService = SPWebService.ContentService;
var moduleModification = new SPWebConfigModification
{
Path = "configuration/system.webServer",
Name = "modules[1=1]/remove[@name='MyModule']",
Sequence = 0,
Owner = "Sample",
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Value = "<remove name='MyModule' />",
};
spWebService.WebConfigModifications.Add(moduleModification);
moduleModification = new SPWebConfigModification
{
Path = "configuration/system.webServer",
Name = "modules[2=2]/add[@name='MyModule'][@type='[type full assembly path]']",
Sequence = 1000,
Owner = "Sample",
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
Value = "<add name='MyModule' type='type full assembly path' />",
};
spWebService.WebConfigModifications.Add(moduleModification);
spWebService.Update();
spWebService.ApplyWebConfigModifications();
});
Also, you might want to double check what happens when you call ApplyWebConfigModifications
to make sure your changes are propagated across the farm. Since you're calling it on an SPWebService I think it is OK but we always use the following:
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
Sources: