public async Task <ResultResponseMessage <ResponseMessage> > QueuesAsync(RequestMessage request)
        {
            IEnumerable <SampleMessage> samplesMessages = new List <SampleMessage>();

            try
            {
                var samplesModelListResult = await SampleService.GetToSendQueueAsync();

                samplesMessages = SampleMessageMapper.FromModel(samplesModelListResult.Models);

                if (samplesMessages.Any())
                {
                    await SendToQueue(samplesMessages, request.GetHeader(Headers.Protocol));
                }

                var result          = new ResultResponseMessage <ResponseMessage>(request);
                var responseMessage = new ResponseMessage();

                responseMessage.AddHeader("Queued-Total", samplesMessages.Count().ToString());
                result.SetReturn(responseMessage);
                result.CreateResponseAccepted();
                return(result);
            }
            catch
            {
                await SampleService.UndoSendQueueAsync(samplesMessages.Select(it => it.IdSample));

                throw;
            }
        }
コード例 #2
0
        public static ResultResponseMessage ToResultResponseMessage <TRequestMessage>(this IModelResult modelResult, TRequestMessage requestMessage)
            where TRequestMessage : RequestMessage
        {
            var result = new ResultResponseMessage(requestMessage);

            if (!modelResult.IsModelResultValid())
            {
                result.MapResultResponseMessage((HttpStatusCode)modelResult.ModelStatusCode, modelResult.Message);
            }

            return(result);
        }
        private void ExecuteValidationInArgument <TRequestMessage>(ActionExecutingContext context, TRequestMessage requestMessage)
            where TRequestMessage : RequestMessage
        {
            Configure.SetProtocolToRequest(context, requestMessage);
            Configure.SetOriginIp(context);
            Configure.SetOriginDevice(context);
            Configure.SetCustomHeaders(context, requestMessage);

            var result = new ResultResponseMessage <ResponseMessage>(requestMessage);

            if (requestMessage == null)
            {
                result.CreateResponseBadRequest("The request cannot be null.");
                context.Result = result;
                return;
            }

            if (context.ModelState.IsValid)
            {
                return;
            }

            foreach (KeyValuePair <string, ModelStateEntry> kvp in context.ModelState)
            {
                string          key   = kvp.Key.Split('.').LastOrDefault();
                ModelStateEntry value = kvp.Value;

                if (value.Errors == null || !value.Errors.Any())
                {
                    continue;
                }

                var errorMessage = value.Errors.First().ErrorMessage;

                key = char.ToLowerInvariant(key[0]) + key.Substring(1);

                if (string.IsNullOrWhiteSpace(errorMessage) || errorMessage.ToLower().Contains("field is required"))
                {
                    result.AddValidation(key, "The field " + key + " is required");
                }
                else
                {
                    result.AddValidation(key, value.Errors.FirstOrDefault()?.ErrorMessage);
                }
            }

            result.CreateResponseBadRequest("Fields Validation");
            context.Result = result;
        }
        private async Task HandleExceptionAsync(HttpContext context, Exception ex)
        {
            if (ex is AggregateException)
            {
                ex = ex.InnerException;
            }

            var errorMessage = ViewDetailsOnResponse ? ex.ToString() : Messages.ErrorDefault;

            var protocol = context.Request.Headers[Headers.Protocol].ToString();

            if (string.IsNullOrWhiteSpace(protocol) || !Guid.TryParse(protocol, out Guid protocolParsed))
            {
                protocol = Guid.NewGuid().ToString("N");

                if (string.IsNullOrWhiteSpace(protocol))
                {
                    context.Request.Headers.Add(Headers.Protocol, protocol);
                }
                else
                {
                    context.Request.Headers[Headers.Protocol] = protocol;
                }
            }

            var requestMessage = new RequestMessage();

            requestMessage.AddHeader(Headers.Protocol, protocol);

            var result = new ResultResponseMessage(requestMessage);

            result.CreateResponseInternalServerError(errorMessage);

            var jsonResult = JsonSerializer.Serialize(result, new JsonSerializerOptions
            {
                IgnoreNullValues = true,
                Encoder          = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            });

            context.Response.ContentType = "application/json";
            context.Response.StatusCode  = (int)result.HttpStatusCode;
            await context.Response.WriteAsync(jsonResult);
        }
コード例 #5
0
        public static ResultResponseMessage ToResultResponseMessage <TRequestMessage, TModel>(this IModelResult <TModel> modelResult, TRequestMessage requestMessage)
            where TRequestMessage : RequestMessage
            where TModel : IModel
        {
            var result = new ResultResponseMessage(requestMessage);

            if (!modelResult.IsModelResultValid())
            {
                if (modelResult.Validations != null)
                {
                    foreach (var validation in modelResult.Validations)
                    {
                        result.AddValidation(validation.Attribute, validation.Message);
                    }
                }

                result.MapResultResponseMessage((HttpStatusCode)modelResult.ModelStatusCode, modelResult.Message);
            }

            return(result);
        }