Loading...
Area: Optimizely CMS
Applies to versions: 12 and higher
Other versions:
ARCHIVED This content is retired and no longer maintained. See the version selector for other versions of this topic.

Implementing a custom event provider

Recommended reading 
Note: This documentation is for the preview version of the upcoming release of CMS 12/Commerce 14/Search & Navigation 14. Features included here might not be complete, and might be changed before becoming available in the public release. This documentation is provided for evaluation purposes only.

The following example shows a simple pseudo code for writing an event provider based on file with options.

You can find more providers, such as the Amazon provider and the Azure provider in the Optimizely NuGet feed.

using EPiServer.Events;
using EPiServer.Events.Providers;
using EPiServer.ServiceLocation;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace CustomEventProvider
{
    [Options]
    public class CustomEventProviderOptions
    {
        public string CustomSetting { get; set; } = "custom setting";
    }

    public class CustomEventProvider : EventProvider
    {
        public CustomEventProvider(CustomEventProviderOptions options)
        {
        }

        public override Task InitializeAsync()
        {
            //Put initialization code here, example waiting for external callback and call OnMessageReceived(messages)
        }

        public override Task SendMessageAsync(EventMessage message, CancellationToken cancellationToken)
        {
            //Put code to sends the provided message to other related sites.
        }
        public override void SendMessage(EventMessage message)
        {
            throw new NotSupportedException("It doesn't need becuase there is already async version.");
        }
    }
}

Configuring an event provider

To add a custom event provider, add the provider configuration to appsettings.json as the following example shows:

{
  "EPiServer" : {
    "Cms" : {
      "EventProviderOptions" : {
        "Provider" : "CustomEventProvider.CustomEventProvider, CustomAssembly"
      }
    },
    "CustomEventProviderOptions" : {
      "CustomSetting" : "custom setting"
    }
  }
}
Do you find this information helpful? Please log in to provide feedback.

Last updated: Jul 02, 2021

Recommended reading