Tobias Nilsson
Mar 3, 2016
  2572
(2 votes)

How to listen to remote Commerce events

This will be a quick tips for people who want to listen to external Commerce events. I have a perculiar situation where I via the Service API import my products, but the service API isn't hosted on the same site/server as my site, but on a separate one instead. This means that I can't listen to local events like I would normally and will have to listen to remote events. But is there such events for Commerce? I know that Episerver sends remote events for cache invalidations, so maybe they send events for Commerce event too.

To listen to local Commerce events you can create your own implementation of CatalogEventListenerBase and one of the out of the box implementation of this abstract class is the CatalogEventBroadcaster. This class will take the local event and send out remote events that we can listen to! So to solve my problem I implemented the following in a IConfigurableModule.

	[ModuleDependency(typeof(ServiceContainerInitialization))]
	public class CatalogEventListener : IConfigurableModule
	{
		public void Initialize(EpiserverCommerce.Framework.Initialization.InitializationEngine context)
		{
			Event ev = Event.Get(CatalogEventBroadcaster.CommerceProductUpdated);
			ev.Raised += Ev_Raised;
		}

		private void Ev_Raised(object sender, EpiserverCommerce.Events.EventNotificationEventArgs e)
		{
			if (!(e.Param is Byte[])) return;
			var eventArgs = DeSerialize(e.Param as Byte[]);
			// Care only about catalog entry events
			var catalogEvents = eventArgs as CatalogContentUpdateEventArgs;
			if (catalogEvents == null || !catalogEvents.CatalogEntryIds.Any()) return;

			switch (catalogEvents.EventType)
			{
				case Mediachase.Commerce.Catalog.Events.CatalogEventBroadcaster.CatalogEntryUpdatedEventType:
					catalogEvents.CatalogEntryIds.ForEach(entryId => UpdateIndex(entryId));
					break;
				default:
					break;
			}
		}

		private static EventArgs DeSerialize(byte[] buffer)
		{
			var formatter = new BinaryFormatter();
			using (var stream = new MemoryStream(buffer))
			{
				return formatter.Deserialize(stream) as EventArgs;
			}
		}		
	}

Big thanks to Quan Mai for helping me find the right class!

Mar 03, 2016

Comments

Vincent
Vincent Jul 14, 2016 02:43 AM

Thanks, Toni. I recently experienced the same issue, Quan pointed me to your blog post. 

Please login to comment.
Latest blogs
Creating an Optimizely CMS Addon - Adding an Editor Interface Gadget

In   Part One   of this series, I covered getting started with creating your own AddOn for Optimizely CMS 12. This covered what I consider to be an...

Mark Stott | Aug 30, 2024

Configure your own Search & Navigation timeouts

The main blog Configure your own Search & Navigation timeouts was posted for years but you need copy the code to your application. We now bring tho...

Manh Nguyen | Aug 30, 2024

Joining the Optimizely MVP Program

Andy Blyth has been honoured as an Optimizely MVP, recognising his contributions to the Optimizely community. Learn how this achievement will enhan...

Andy Blyth | Aug 29, 2024 | Syndicated blog

Welcome 2024 Summer OMVPs

Hello, Optimizely community! We are thrilled to announce and welcome the newest members to the Optimizely Most Valuable Professionals (OMVP) progra...

Patrick Lam | Aug 29, 2024

Create custom folder type in Edit Mode

Content Folders, which are located in assets pane gadget or multichannel content gadget, allow you to add any type of block or folders. But...

Grzegorz Wiecheć | Aug 28, 2024 | Syndicated blog

Creating an Optimizely AddOn - Getting Started

When Optimizely CMS 12 was launched in the summer of 2021, I created my first AddOn for Optimizely CMS and talked about some of the lessons I learn...

Mark Stott | Aug 28, 2024