ERROR Exception while handling event Sitecore.Eventing.Remote.PublishEndRemoteEventException: System.AggregateExceptionMessage: One or more exceptions occurred while processing the subscribers to the 'publish:end:remote'

I was getting below error while working with cache management on CM and CD server. Actually my code was working fine on CM server but I was not able to access or clear cache object on CD server because of wrong event selection. 

PID[18108] Error       ManagedPoolThread #3 16:24:16 ERROR Exception while handling event Sitecore.Eventing.Remote.PublishEndRemoteEventException: System.AggregateExceptionMessage: One or more exceptions occurred while processing the subscribers to the 'publish:end:remote' event.Source: Sitecore.Kernelat Sitecore.Events.Event.EventSubscribers.RaiseEvent(String eventName, Object[] parameters, EventResult result)at Sitecore.Events.Event.RaiseEvent(String eventName, Object[] parameters)at Sitecore.Eventing.Subscription`1.Invoke(Object instance, EventContext context)at Sitecore.Eventing.EventProvider.RaiseEvent(Object event, Type eventType, EventContext context)


Above error comes due to wrong event args access in CD server. There are two event args in case of publish event one is SitecoreEventArgs that is for CM server and PublishEndRemoteEventArgs for remote server. To clear custom cache object on both CM and CD servers I just write two different methods and handle both publish event( publish.end, publish.end.remote).


static SharjeelsWorkCacheManager()
        {
            SharjeelsWorkCache = new SharjeelsWorkCustomCache(SharjeelsWorkInventoryCache, StringUtil.ParseSizeString("20KB"));
            //subscribe events for content publishing end on remote server. Also remove CM publish end as it was for testing purpose only
            Sitecore.Events.Event.Subscribe("publish:end", OnPublishEnd);
            Sitecore.Events.Event.Subscribe("publish:end:remote", OnPublishEndRemote);
        }


        /// <summary>
        /// clear cache of cached object from remote server.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        static void OnPublishEnd(object sender, System.EventArgs eventArgs)
        {
            if (eventArgs != null)
            {
                try
                {
                    SitecoreEventArgs scEventArgs = eventArgs as SitecoreEventArgs;
                    //get current item from event args
                    Item publishedItem = ((Sitecore.Publishing.Publisher)(scEventArgs.Parameters[0])).Options.RootItem as Item;

                    // check published item is a samplekit item or not. if it's a sample kit item then clear cache object
                    if (publishedItem != null && publishedItem.TemplateName == ISharjeelsWorkKitConstants.TemplateName && SharjeelsWorkCache != null)
                    {
                        SharjeelsWorkCache.Clear();
                    }
                }
                catch (Exception exInfo)
                {
                    Sitecore.Diagnostics.Log.Error("ERROR:: " + exInfo.Message.ToString(), "SharjeelsWorkCacheManager : OnPublishEnd");
                }
            }
        }

        /// <summary>
        /// clear cache of cached object from remote server.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        static void OnPublishEndRemote(object sender, System.EventArgs eventArgs)
        {
            if (eventArgs != null)
            {
                try
                {
                    PublishEndRemoteEventArgs scRemoteEventArgs = eventArgs asPublishEndRemoteEventArgs;

                    //get current item from remote event args
                    Item publishedItem = SiteExtensions.GetItemById(newSitecore.Data.ID(scRemoteEventArgs.RootItemId));

                    // check published item is a samplekit item or not. if it's a sample kit item then clear cache object
                    if (publishedItem != null && publishedItem.TemplateName == ISharjeelsWorkKitConstants.TemplateName && SharjeelsWorkCache != null)
                    {
                        SharjeelsWorkCache.Clear();
                    }
                }
                catch (Exception exInfo)
                {
                    Sitecore.Diagnostics.Log.Error("ERROR:: " + exInfo.Message.ToString(), " SharjeelsWorkCacheManager : OnPublishEndRemote ");
                }
            }
        }


Now one method OnPublishEnd will work in case of CM server content publish event and method OnPublishEndRemote will work in case of remote server.



Happy Learning!!!

Comments

Popular posts from this blog

Error : DependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: com.adobe.aem:uber-jar:jar:apis -> version 6.3.0 vs 6.4.0

Operators in Asterisk with Linux