Пример #1
0
        public async Task <long> GetEventsAsync(string actorName, long indexStart, long indexEnd,
                                                Action <object> callback)
        {
            StreamEventsSlice slice;
            var start = indexStart;

            do
            {
                var count = (int)Math.Min(indexEnd - start, 199) + 1;
                slice = await connection.ReadStreamEventsForward(actorName, start, count, false);

                foreach (var resolvedEvent in slice.Events)
                {
                    var data = resolvedEvent.Event.Data;
                    if (resolvedEvent.Event.Metadata.Length > 0)
                    {
                        var metadata = JObject.Parse(Encoding.UTF8.GetString(resolvedEvent.Event.Metadata));
                        if (metadata["encoding"].Value <string>() == "gzip")
                        {
                            using (var originalStream = new MemoryStream())
                            {
                                using (var compressedStream = new MemoryStream(data))
                                    using (var gZipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
                                    {
                                        gZipStream.CopyTo(originalStream);
                                    }

                                data = originalStream.ToArray();
                            }
                        }
                    }

                    var eventType = Type.GetType($"SM.Service.Messages.{resolvedEvent.Event.EventType}");
                    var message   = (IMessage)Activator.CreateInstance(eventType);

                    message.MergeFrom(data);
                    callback(message);
                }

                start = slice.NextEventNumber;
            } while (start <= indexEnd && !slice.IsEndOfStream);

            return(slice.NextEventNumber);
        }
Пример #2
0
        public async Task <long> GetEventsAsync(string actorName, long indexStart, long indexEnd,
                                                Action <object> callback)
        {
            StreamEventsSlice slice;
            var start = indexStart;

            do
            {
                var count = (int)Math.Min(indexEnd - start, 199) + 1;
                slice = await connection.ReadStreamEventsForward(actorName, start, count, false);

                foreach (var resolvedEvent in slice.Events)
                {
                    callback(resolvedEvent.Event.ToMessage());
                }

                start = slice.NextEventNumber;
            } while (start <= indexEnd && !slice.IsEndOfStream);

            return(slice.NextEventNumber);
        }