Try our conversational search powered by Generative AI!

Tobias Nilsson
Mar 3, 2016
  2507
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024