Ejemplo n.º 1
0
        public async Task CreateEvent(Guid eventId, string eventType = "DetailsView")
        {
            var @event = new
            {
                EventId   = eventId,
                Timestamp = DateTimeFactory.Now().ToString("O"),
                EventType = eventType
            };

            var eventJson      = JsonConvert.SerializeObject(@event, _jsonSettings);
            var requestContent = new StringContent(eventJson, Encoding.UTF8, "application/json");
            var request        = new HttpRequestMessage(HttpMethod.Post, "/events")
            {
                Content = requestContent
            };

            var response = await _httpClient.SendAsync(request);

            try
            {
                var statusCode = response.StatusCode;
                if (statusCode == HttpStatusCode.Created)
                {
                    return;
                }

                await RaiseResponseError(request, response);
            }
            finally
            {
                Dispose(request, response);
            }
        }
Ejemplo n.º 2
0
        public void CreateEvent(Guid eventId, string eventType = "DetailsView")
        {
            HttpStatusCode statusCode;
            string         reasonPhrase;

            using (var client = HttpClient())
            {
                var @event = new
                {
                    EventId   = eventId,
                    Timestamp = DateTimeFactory.Now().ToString("O"),
                    EventType = eventType
                };

                var eventJson = JsonConvert.SerializeObject(@event, _jsonSettings);

                var requestContent = new StringContent(eventJson, Encoding.UTF8, "application/json");

                var request = new HttpRequestMessage(HttpMethod.Post, "/events");
                request.Content = requestContent;

                var response = client.SendAsync(request);
                var result   = response.Result;
                statusCode   = result.StatusCode;
                reasonPhrase = result.ReasonPhrase;

                request.Dispose();
                response.Dispose();
                result.Dispose();
            }

            if (statusCode != HttpStatusCode.Created)
            {
                throw new InvalidOperationException(reasonPhrase);
            }
        }