예제 #1
0
        public IEnumerable <object> ReadRaw(string streamId, bool untilLastSnapshot = true)
        {
            var eventDatas = eventsStorage.Read(streamId);

            if (eventDatas == null)
            {
                yield break;
            }

            foreach (var data in eventDatas)
            {
                var typeString = data.Metadata.Value <string>("Clr-Type");
                var clrType    = FindClrType(typeString);
                yield return(Serializer.Deserialize(new JTokenReader(data.Data), clrType));

                if (data.State.HasFlag(EventState.Snapshot) && untilLastSnapshot)
                {
                    yield break;
                }
            }
        }
예제 #2
0
        private async void ListenAsync()
        {
            var ctx = await Task.Factory.FromAsync <HttpListenerContext>(httpListener.BeginGetContext, httpListener.EndGetContext, null);

            try
            {
                ListenAsync();
            }
            catch (ObjectDisposedException)
            {
                // disposed, exiting
            }

            //Console.WriteLine(ctx.Request.HttpMethod + " " + ctx.Request.RawUrl);
            var streamWriter = new StreamWriter(ctx.Response.OutputStream);

            try
            {
                var id = ctx.Request.QueryString["id"];
                switch (ctx.Request.HttpMethod)
                {
                case "PUT":
                    var value = (JObject)JToken.ReadFrom(new JsonTextReader(new StreamReader(ctx.Request.InputStream)));
                    if (string.IsNullOrWhiteSpace(id))
                    {
                        throw new ArgumentException("id query string must have a value");
                    }

                    var stateStr = ctx.Request.QueryString["state"];

                    EventState state;
                    Enum.TryParse(stateStr, out state);

                    var metadata = value.Value <JObject>("@metadata");
                    value.Remove("@metadata");

                    await data.EnqueueAsync(id, state, value, metadata);

                    break;

                case "GET":
                    if (string.IsNullOrWhiteSpace(id))
                    {
                        throw new ArgumentException("id query string must have a value");
                    }

                    var stream = data.Read(id);
                    if (stream == null)
                    {
                        ctx.Response.StatusCode        = 404;
                        ctx.Response.StatusDescription = "Not Found";
                        return;
                    }
                    var writer = new JsonTextWriter(streamWriter);
                    writer.WriteStartObject();
                    writer.WritePropertyName("Stream");
                    writer.WriteStartArray();


                    foreach (var item in stream)
                    {
                        writer.WriteStartObject();

                        writer.WritePropertyName("@metadata");
                        writer.WriteStartObject();
                        writer.WritePropertyName("State");
                        writer.WriteValue(item.State);
                        writer.WriteEndObject();

                        item.Data.WriteTo(writer);
                        writer.WriteEndObject();
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();

                    break;

                default:
                    throw new NotSupportedException("Http Method: " + ctx.Request.HttpMethod);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                ctx.Response.StatusCode = 500;
                using (var wr = streamWriter)
                {
                    wr.Write(e);
                }
            }
            finally
            {
                streamWriter.Flush();
                ctx.Response.Close();
            }
        }