HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Create a custom event provider

Create and configure a custom event provider for Optimizely CMS 13 by extending the EventProvider base class and registering it in the service container.

Create a custom event provider when you need a transport mechanism other than the built-in or Azure options. Extend the EventProvider base class from EPiServer.Events.Providers, implement the required methods, and register the provider in the service container at startup. Prebuilt providers for Amazon and Azure are available on the Optimizely NuGet feed.

The following example defines a custom event provider with a configurable options class:

using EPiServer.Events;
using EPiServer.Events.Providers;
using Microsoft.Extensions.Options;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace CustomEventProvider;

public class CustomEventProviderOptions 
{
    public string CustomSetting { get; set; } = "custom setting";
}

public class CustomEventProvider : EventProvider 
{
    public CustomEventProvider(IOptions<CustomEventProviderOptions> options) {}

    public override Task InitializeAsync() 
    {
        // Initialize the provider and call OnMessageReceived(messages) when messages arrive
    }

    public override Task SendMessageAsync(EventMessage message, CancellationToken cancellationToken) 
    {
        // Send the message to other instances
    }
  
    public override void SendMessage(EventMessage message) 
    {
        throw new NotSupportedException("Use SendMessageAsync instead.");
    }
}

Register the custom event provider

Register the custom event provider in Startup.cs and configure its options. The following example replaces the default provider with MyEventProvider:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCmsEvents();
    services.AddSingleton<EventProvider, MyEventProvider>();
    services.Configure<CustomEventProviderOptions>(o => o.CustomSetting = "some value");
}