Tobias Nilsson
Mar 3, 2016
  2579
(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 Addon - Packaging for NuGet

In   Part One   and   Part Two   of this series; I covered topics from having a great idea, solution structure, extending the menus and adding...

Mark Stott | Sep 16, 2024

Optimizely CMS and weekly updates

Learn how reporting bugs in Optimizely CMS not only helps improve the platform but also benefits you and the entire user community.

Tomas Hensrud Gulla | Sep 12, 2024 | Syndicated blog

Introduce the ablility to select then delete items manually on FIND UI

In FIND 16.3.0 we introduce an ability to select items and delete them manually, it will helps you to delete unexpected items from the UI without a...

Manh Nguyen | Sep 12, 2024

The composable consulting model our industry needs

The architecture of a modern consulting business is ‘composable’. Certainly, we think of ourselves a composable consulting business and have done...

Mark Everard | Sep 12, 2024 | Syndicated blog

Keynote Summary from Opticon 2024, Stockholm

At Opticon in Stockholm, marking the 30th anniversary of Optimizely, the company celebrated significant achievements. These included surpassing $40...

Luc Gosso (MVP) | Sep 11, 2024 | Syndicated blog

#Onederful!

Opticon Stockholm has finished and my journey back to Yorkshire from #Onederland (via Oslo) has given me space to reflect. Here are the Top (or...

Mark Everard | Sep 11, 2024 | Syndicated blog