In Sitecore config patch files, both of the following will give the same result:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
xmlns:role="http://www.sitecore.net/xmlconfig/role/"
xmlns:environment="http://www.sitecore.net/xmlconfig/environment/">
<sitecore role:require="ContentManagement or Standalone">
<scheduling>
<agent type="MyAgent, MyAssembly" method="RunMyAgent" interval="00:60:00" />
<agent type="MyAgent, MyAssembly" environment:require="dev">
<patch:attribute name="interval">00:00:00</patch:attribute>
</agent>
</scheduling>
</sitecore>
</configuration>
and a less conventional technique:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
xmlns:role="http://www.sitecore.net/xmlconfig/role/"
xmlns:environment="http://www.sitecore.net/xmlconfig/environment/">
<sitecore role:require="ContentManagement or Standalone">
<scheduling>
<agent type="MyAgent, MyAssembly" method="RunMyAgent" interval="00:60:00" />
<agent type="MyAgent, MyAssembly" interval="00:00:00" environment:require="dev" />
</scheduling>
</sitecore>
</configuration>
In dev/standalone, these both result in:
<agent type="MyAgent, MyAssembly" method="RunMyAgent" interval="00:00:00" patch:source="MyConfigPatch.config"/>
I'm curious about that last syntax. Based on the Sitecore documentation, you might expect it to add two agents, but instead it accurately patches the interval, all the while keeping the method
attribute intact:
When Sitecore applies the changes from a patch file, it tries to match each element in the patch file to an element in the existing configuration using the combination of the element name and all the attributes of the element. If there is a match, Sitecore updates the existing element. If there is no match, Sitecore inserts a new element.
What is the exact magic taking place here? How does Sitecore know which attribute to match (type
), and which one to patch (interval
)? (Note that this also works if I reverse the order of the attributes.) Is there some intelligence based on the context (agents) if one attribute is declared as unique?
For instance, why is the agent MyAgent
being patched here?
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
xmlns:role="http://www.sitecore.net/xmlconfig/role/"
xmlns:environment="http://www.sitecore.net/xmlconfig/environment/">
<sitecore role:require="ContentManagement or Standalone">
<scheduling>
<agent interval="00:00:00" type="MyOtherAgent, MyOtherAssembly" method="RunMyOtherAgent" />
<agent type="MyAgent, MyAssembly" method="RunMyAgent" interval="00:60:00" />
<!-- Why does this patch "interval" on the second agent, and not "type" on the first? -->
<agent interval="00:00:00" type="MyAgent, MyAssembly" environment:require="dev" />
</scheduling>
</sitecore>
</configuration>
It seems intuitive: obviously you're not going to want to match a configuration node based on interval, but how does Sitecore infer this? In XDT, you have to specify which attribute(s) to match, but Sitecore seems to have more intelligence.