Domanda

Ho un codice che utilizza SPWebConfigModification per aggiungere un paio di nodi alla sezione System.Webserver / Modules. Quello che voglio fare è aggiungere un <remove name="MyModule" /> e poi un <add name="MyModule" type="[type full assembly path]">.

Non importa quello che sembro fare però, il web.config finisce sempre con il <add ../> prima del <remove ../>. Questo è ovviamente errato, poiché il modulo verrà rimosso dopo che è stato aggiunto. Quello che voglio fare è rimuoverlo, quindi aggiungilo. Ecco cosa fa il web.config per sembrare come:

<configuration>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
            <add name="MyModule" type="[type full assembly path]" />
            <remove name="MyModule" />
        </modules>
    </system.webServer>
</configuration>
.

Ecco cosa voglio che sembri:

<configuration>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
            <remove name="MyModule" />
            <add name="MyModule" type="[type full assembly path]" />
        </modules>
    </system.webServer>
</configuration>
.

.. ed ecco il codice, che viene eseguito durante SpfeatureuretureReceiver.CoatureSstalled:

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();
});
.

... so che dovrei essere in grado di fare entrambe queste modifiche prima di chiamare l'aggiornamento e applywebconfigmodifications, ma solo questo codice qui in questo modo perché non riesco a capire perché sta mettendo l'aggiunta sopra il rimuovere. Perché lo fa così? Sta cercando di alfabetizzare i nodi? Come posso specificare l'ordine?

È stato utile?

Soluzione

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:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top