EPiWiki.se  - EPiServer notes shared with others
 

Send and receive remote events

[Edit]
Remote events in EPiServer CMS are very useful when having a larger solution with several servers

Sending event



public static readonly Guid MyRraiserId =
   new Guid("2528CCC5-D6D3-47b2-A151-FC05A480039C");
public static readonly Guid MyEventId =
   new Guid("89B4DC98-8736-44ed-905F-7C05F409C445");

EPiServer.Events.Clients.Event.Get(MyEventId)
   .Raise(MyRraiserId, "Hello server!"));


Receiving event register to the event handler in global.asax



public override void Init()
{
   base.Init();

   EPiServer.Web.InitializationModule.FirstBeginRequest +=
      new EventHandler(InitializationModule_FirstBeginRequest);
}

void InitializationModule_FirstBeginRequest(
   object sender,
   EventArgs e)
{
   EPiServer.Events.Clients.Event.Get(MyRraiserId).Raised
             += new EPiServer.Events.EventNotificationHandler(MyEventRaised);
}

void MyEventRaised(object sender, EPiServer.Events.EventNotificationEventArgs e)
{
   log4net.LogManager.GetLogger(GetType())
      .DebugFormat("Receiving event: {0}", e.Param);
}


Hijacking written events


Create your own ReplicationProvider and listen to all send events.

using System;
using EPiServer.Events;
using EPiServer.Events.Remote;
using EPiServer.Events.ServiceModel;
using log4net;

namespace EPiServer
{
   public class MyEventReplicationProvider
      : EventReplicationProvider, IEventReplication
   {
      private static readonly ILog _log =
         LogManager.GetLogger(typeof(MyEventReplicationProvider));

      public override ServiceModel.IEventReplication NewReplicator()
      {
          return this;
      }

      public void RaiseEvent(
        Guid raiserId,
        string siteId,
        int sequenceNumber,
        byte[] verificationData,
        Guid eventId,
        object param)
        {
            _log.DebugFormat("{0} {1} {2} {3} {4} {5}",
                raiserId,
                siteId,
                sequenceNumber,
                verificationData,
                eventId,
                param);
        }
    }
}

Version author:
Mattias Lövström

EPiServer version

'EPiServer CMS 5'