Persisting Events not working

Hi guys, I want to start using the event store, but have some issues with it.

Here is the method that persists the event:

        private void AppendEvent(Guid id, string type, byte[] data, byte[] metadata)
        {
            var stream = GetStreamName(id, type);
            CreateStream(id, type, metadata);
            var evnt = new Event(id, type, data, metadata);
            var evnts = new List<Event>();
            evnts.Add(evnt);
            EventStore.ClientAPI.EventStore.AppendToStream(stream, 0, evnts);
        }

``

Here is the method that reads the persisted events:

        public IList<EventEnvelope> ReadEvents(Guid id, string type, int fromVersion, int toVersion)
        {
            var stream = GetStreamName(id, type);
            var slice = EventStore.ClientAPI.EventStore.ReadEventStream(stream, fromVersion, toVersion);
 
            IList<Event> tmp =
                slice.Events.Select(
                    recordedEvent =>
                    new Event(recordedEvent.EventId, recordedEvent.EventType, recordedEvent.Data, recordedEvent.Metadata))
                    .ToList();
 
            return tmp.Select(e => DeserializeEventEnvelope(e.Data)).ToList();
        }

``

The event is read but the Data field is empty. At serialization time the Data field is not empty, so something happens in the event store and I am not getting the same thing I put in it. This happens on the master branch.

Can you try on dev branch? There are a ton of ClientAPI changes there.

It is the same on the dev branch.

Can you send me a test using using only the client API and byte arrays?

or alternatively (we dont use the helper class)

using(var conn = new EventStoreConnection(....)) {

}

We have never had anyone have issues with data not being saved previously

or put up the rest of the sample code and I will run it locally.

This works without any issue against dev/master.

Output is:

received event $stream-created
data length 0

received event TEST1
data length 11

[127.0.0.1:1113]:
Received packages: 2, bytes: 451
Send packages: 2, bytes: 219
SendAsync calls: 2, callbacks: 2
ReceiveAsync calls: 3, callbacks: 2

At the same time I figured out what your issue likely is (the stream
created event!)

Code Follows.

Greg

    class Program
    {
        class MyEvent : Event
        {
            public Guid EventId { get; set; }

            public string Type { get; set; }

            public byte[] Data { get; set; }

            public byte[] Metadata { get; set; }
        }
        static void AppendEvent(EventStoreConnection conn, Guid id,
string type, byte[] data, byte[] metadata)
        {
            var stream = "TEST " + id;

            var evnt = new MyEvent() {EventId = id, Type=type,
Data=data, Metadata = metadata};
            var evnts = new List<Event> {evnt};
            conn.AppendToStream(stream, ExpectedVersion.Any, evnts);
        }

        static IEnumerable<MyEvent> ReadEvents(EventStoreConnection
conn, Guid id, int fromVersion, int toVersion)
        {
            var stream = "TEST "+ id;
            var slice = conn.ReadEventStream(stream, fromVersion, toVersion);

            IList<MyEvent> tmp =
                slice.Events.Select(
                    recordedEvent =>
                    new MyEvent() {Type=recordedEvent.EventType, Data
= recordedEvent.Data, EventId = recordedEvent.EventId})
                    .ToList();

            return tmp;
        }
        static void Main(string[] args)
        {
            using (var conn = new EventStoreConnection(new
IPEndPoint(IPAddress.Parse("127.0.0.1"), 1113)))
            {
                var id = Guid.NewGuid();
                AppendEvent(conn, id, "TEST1",
Encoding.ASCII.GetBytes("HELLO WORLD"), new byte[0]);
                var read = ReadEvents(conn, id, 0, 2);
                foreach (var myEvent in read)
                {
                    Console.WriteLine("received event " + myEvent.Type);
                    Console.WriteLine("data length " + myEvent.Data.Length);
                    Console.WriteLine();
                }
            }
        }
    }

to be clear on what you were seeing. Event 0 in a stream is always the
StreamCreated event that never has as body but can have metadata. Your
events start at Event 1. Your code was trying to deserialize the 0
event I imagine.

Cheers,

Greg

Thank you for the Help, the last post helped me to fix the issue.