To do that I have implemented a custom remote event. I did not find some documentation on sdn so here is the procedure (thank you to the Sergey Kravchenko from the sitecore support).
- Create your custom remote event class. Remote event class is a usual class inherited from System.Object and marked with a [DataContract] attribute.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; namespace XXX { [DataContract] public class ClearCacheEvent { public ClearCacheEvent() { } } } - Create a custom event arguments class, to store the arguments of the event. It also, can be empty. Both Remote event class and event arguments class will be used to add event to the EventQueue.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sitecore.Events; namespace XXX { public class ClearCacheEventArgs : EventArgs, IPassNativeEventArgs { public ClearCacheEventArgs(ClearCacheEvent @event) { } } } - Create your custom event handler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sitecore.Diagnostics; using Sitecore.Events; namespace XXX { public class ClearCacheEventHandler { /// <summary> /// The methos is the method that you need to implement as you do normally /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public virtual void OnClearCacheRemote(object sender, EventArgs e) { Log.Info("Tadaa", this); } /// <summary> /// This methos is used to raise the local event /// </summary> /// <param name="event"></param> public static void Run(ClearCacheEvent @event) { Log.Info("ClearCacheEventHandler - Run", typeof(ClearCacheEventHandler)); ClearCacheEventArgs args = new ClearCacheEventArgs(@event); Event.RaiseEvent("clearcache:remote", new object[] { args }); } } } - Create a custom Hook to bind event from the EventQueue and the "local event" in the CD server web config. This hook will call the method you have added in the event handler ("Run" method of the ClearCacheEventHandler class in our case)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sitecore.Events.Hooks; namespace XXX { public class ClearCacheHook : IHook { public void Initialize() { Sitecore.Eventing.EventManager.Subscribe(new Action (ClearCacheEventHandler.Run)); } } } - Trigger the event in your code
ClearCacheEvent inst = new ClearCacheEvent(); Sitecore.Eventing.EventManager.QueueEvent<ClearCacheEvent>(inst);
- Configure the web.config (or add it in a separated file in the /include folder it is cleaner)
The hook:
<hooks> <hook type="XXX.ClearCacheHook, XXX"/> </hooks>The event:
<events> <event name="clearcache:remote"> <handler type="XXX.ClearCacheEventHandler, XXX" method="OnClearCacheRemote"/> </event> </events>
How does it work?
- Your code add an event in the EventQueue.
- The EventQueue is checked by the special task:
<eventQueue> <processingInterval>00:00:02</processingInterval> </eventQueue> - The Hook call a method who raise the corresponding local event.
- Your local is triggered.