U4-8973 - ContentService Publish event is called before XML cache is updated.

Created by Harvey Williams 13 Sep 2016, 14:33:58 Updated by Markus Johansson 03 Sep 2018, 15:30:18

When a custom event is added to the ContentService.Published event handler, the custom event will fire before the XML cache is updated.

This causes problems if you try to access that node in the custom publish event because:

  • if the node has not been published before, you cannot access it through the UmbracoHelper (the IPublishedContent will be null)
  • if the node has been published before, accessing it through the UmbracoHelper will get you the older version rather than the new version

The following code should illustrate this:

namespace My.Namespace
    public class MyEventHandler : ApplicationEventHandler

        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            ContentService.Published += ContentServicePublished;     

        private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args)
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

            foreach (var e in args.PublishedEntities)
                var node = umbracoHelper.TypedContent(e.Id))
                // node will be null if this is the first time it is being published
                // or it will be an old version
                LogHelper.Info(MethodBase.GetCurrentMethod().DeclaringType, "Publish event for " + e.Id + ".");

If we look at the log, we can see that the logged message in the event occurs before the XML cache is updated:

 2016-09-13 15:11:58,254 [P8264/D12/T560] INFO  My.Namespace.MyEventHandler - Publish event for ####.
 2016-09-13 15:12:31,235 [P8264/D12/T482] INFO  umbraco.content - Save Xml to file...
 2016-09-13 15:12:31,285 [P8264/D12/T560] INFO  umbraco.content - Saved Xml to file.

The Publish event is documented here: https://our.umbraco.org/documentation/reference/events/ContentService-Events

This issue has been brought up (with a workaround) in the forum: https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/55774-Published-Event-How-to-get-Url-Elastic-Search

I would have thought that there is no difference between a node being published and being in the cache - if a node has been published, then it must be in the cache.

I think the XML cache is supposed to be updated on https://github.com/umbraco/Umbraco-CMS/blob/release-7.4.1/src/Umbraco.Core/Services/ContentService.cs#L2007, and the event is supposed to be raised on line 2014.

I'm guessing there is some sort of race condition going on here.



Markus Johansson 03 Sep 2018, 15:30:18

Totally agree with you @Harvey about this one!

Priority: Minor

Type: Bug

State: Submitted


Difficulty: Normal

Category: Extensibility

Backwards Compatible: True

Fix Submitted:

Affected versions: 7.1.4, 7.4.1, 7.9.6

Due in version:


Story Points: