HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Implementing a custom event provider

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"
        }
      }
    }

Did this page help you?