Skip to content

Instantly share code, notes, and snippets.

Created Feb 15, 2018
What would you like to do?
private async Task RaiseEvent(BankAccountEvent evnt)
await ConfirmEvents();
await _conn.AppendToStreamAsync(_stream, Version - 2, ToEventData(Guid.NewGuid(), evnt, new Dictionary<string, object>()));
private static EventData ToEventData(Guid eventId, object evnt, IDictionary<string, object> headers)
var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(evnt));
var eventHeaders = new Dictionary<string, object>(headers)
"EventClrType", evnt.GetType().AssemblyQualifiedName
var metadata = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(eventHeaders));
var typeName = evnt.GetType().Name;
return new EventData(eventId, typeName, true, data, metadata);
private static object DeserializeEvent(RecordedEvent evntData)
var eventClrTypeName = JObject.Parse(Encoding.UTF8.GetString(evntData.Metadata)).Property("EventClrType").Value;
return JsonConvert.DeserializeObject(Encoding.UTF8.GetString(evntData.Data), Type.GetType((string)eventClrTypeName));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment