For my next project we’ll use Event Sourcing. Therefore I’m spending some free time in looking at NEventStore, a persistence agnostic Event Store for .NET.
One of the features of NEventStore is a built-in PollingClient allowing you to poll the Event Store for the latest commits. However when I tried to test the PollingClient nothing happened?!
Here is the code I was using:
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (var store = WireupEventStore()) | |
{ | |
var client = new NEventStore.Client.PollingClient(store.Advanced); | |
string checkpointToken = "6"; | |
using (IObserveCommits observeCommits = client.ObserveFrom(checkpointToken)) | |
using (observeCommits.Subscribe(new ReadModelCommitObserver(new DelegateMessageDispatcher(DispatchCommit)))) | |
{ | |
observeCommits.Start(); | |
Console.WriteLine("hit any key"); | |
Console.ReadKey(); | |
} | |
} | |
} | |
private static void DispatchCommit(ICommit commit) | |
{ | |
// This is where we'd hook into our messaging infrastructure, such as NServiceBus, | |
// MassTransit, WCF, or some other communications infrastructure. | |
// This can be a class as well--just implement IDispatchCommits. | |
try | |
{ | |
foreach (EventMessage @event in commit.Events) | |
Console.WriteLine("Message dispatched"); | |
} | |
catch (Exception) | |
{ | |
Console.WriteLine("Unable to dispatch"); | |
} | |
Console.WriteLine("Checkpoint token= " + commit.CheckpointToken); | |
} | |
private static IStoreEvents WireupEventStore() | |
{ | |
var store = Wireup.Init() | |
.UsingSqlPersistence("NEventStoreSample") | |
.WithDialect(new MsSqlDialect()) | |
.WithStreamIdHasher(s => s) | |
.InitializeStorageEngine() | |
.TrackPerformanceInstance("NEventStoreSample") | |
.UsingJsonSerialization() | |
.Compress() | |
.Build(); | |
return store; | |
} | |
} |
public class ReadModelCommitObserver : IObserver<ICommit> | |
{ | |
private readonly IDispatchCommits dispatcher; | |
public ReadModelCommitObserver( IDispatchCommits dispatcher) | |
{ | |
this.dispatcher = dispatcher; | |
} | |
public void OnCompleted() | |
{ | |
Console.WriteLine("commit observation completed."); | |
} | |
public void OnError(Exception error) | |
{ | |
Console.WriteLine("Exception from ReadModelCommitObserver: {0}", error.Message); | |
throw error; | |
} | |
public void OnNext(ICommit commit) | |
{ | |
dispatcher.Dispatch(commit); | |
} | |
} |
No exception was thrown, however when I browsed through the Output window in Visual Studio I noticed the following message:
A first chance exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in NEventStore.dll
It seems that the PollingClient immediatelly tries to deserialize the committed events, as I didn’t include a reference to the assembly containing the Events, the deserializer (silently) fails.
To fix it, the only thing I had to do was adding a reference to my Events assembly.