private async Task PublishCommandAsync(string name, int version, IOwinContext context)
        {
            _log.Verbose($"Publishing command '{name}' v{version} from OWIN middleware");

            string requestJson;

            using (var streamReader = new StreamReader(context.Request.Body))
            {
                requestJson = await streamReader.ReadToEndAsync().ConfigureAwait(false);
            }

            try
            {
                var sourceId = await _serializedCommandPublisher.PublishSerilizedCommandAsync(
                    name,
                    version,
                    requestJson,
                    CancellationToken.None)
                               .ConfigureAwait(false);
                await WriteAsync(
                    new
                {
                    SourceId = sourceId.Value,
                },
                    HttpStatusCode.OK,
                    context)
                .ConfigureAwait(false);
            }
            catch (ArgumentException e)
            {
                _log.Debug(e, $"Failed to publish serilized command '{name}' v{version} due to: {e.Message}");
                await WriteErrorAsync(e.Message, HttpStatusCode.BadRequest, context).ConfigureAwait(false);
            }
            catch (DomainError e)
            {
                await WriteErrorAsync(e.Message, HttpStatusCode.BadRequest, context).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                _log.Error(e, $"Unexpected exception when executing '{name}' v{version}");
                await WriteErrorAsync("Internal server error!", HttpStatusCode.InternalServerError, context).ConfigureAwait(false);
            }
        }
Esempio n. 2
0
        private async Task PublishCommandAsync(String name, Int32 version, HttpContext context)
        {
            _log.LogTrace($"Publishing command '{name}' v{version} from OWIN middleware");
            String requestJson;

            using (var streamReader = new StreamReader(context.Request.Body))
                requestJson = await streamReader.ReadToEndAsync().ConfigureAwait(false);
            try
            {
                var result = await _serializedCommandPublisher.PublishSerilizedCommandAsync(name, version, requestJson, CancellationToken.None).ConfigureAwait(false);
                await WriteAsync(result, HttpStatusCode.OK, context).ConfigureAwait(false);
            }
            catch (ArgumentException ex)
            {
                _log.LogDebug(ex, $"Failed to publish serialized command '{name}' v{version} due to: {ex.Message}");
                await WriteErrorAsync(ex.Message, HttpStatusCode.BadRequest, context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _log.LogError(ex, $"Unexpected exception when executing '{name}' v{version}");
                await WriteErrorAsync("Internal server error!", HttpStatusCode.InternalServerError, context).ConfigureAwait(false);
            }
        }