Ejemplo n.º 1
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            HttpRequest request = context.Request;

            string queryName = GetQueryName(request);

            if (QueryTypes.ContainsKey(queryName))
            {
                // GET operations get their data through the query string, while POST operations expect a JSON
                // object being put in the body.
                string queryData = request.Method.Equals("get", StringComparison.OrdinalIgnoreCase)
                    ? SerializationHelpers.ConvertQueryStringToJson(request.QueryString.Value)
                    : request.Body.ReadToEnd();

                QueryInfo info = QueryTypes[queryName];

                Type handlerType = typeof(IQueryHandler <,>).MakeGenericType(info.QueryType, info.ResultType);

                this.ApplyHeaders(request);

                dynamic handler = this.handlerFactory.Invoke(handlerType);

                try
                {
                    dynamic query = JsonConvert.DeserializeObject(
                        string.IsNullOrWhiteSpace(queryData) ? "{}" : queryData,
                        info.QueryType,
                        this.jsonSettings);

                    object result = handler.Handle(query);

                    await context.WriteResultAsync(new ObjectResult(result));
                }
                catch (Exception exception)
                {
                    ObjectResult response =
                        WebApiErrorResponseBuilder.CreateErrorResponseOrNull(exception, context);

                    if (response != null)
                    {
                        await context.WriteResultAsync(response);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            else
            {
                var response = new ObjectResult(queryName)
                {
                    StatusCode = StatusCodes.Status404NotFound
                };

                await context.WriteResultAsync(response);
            }
        }
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            HttpRequest request = context.Request;

            string commandName = GetCommandName(request);

            if (request.Method == "POST" && CommandTypes.ContainsKey(commandName))
            {
                Type commandType = CommandTypes[commandName];

                string commandData = request.Body.ReadToEnd();

                Type handlerType = typeof(ICommandHandler <>).MakeGenericType(commandType);

                this.ApplyHeaders(request);

                dynamic handler = this.handlerFactory.Invoke(handlerType);

                try
                {
                    dynamic command = JsonConvert.DeserializeObject(
                        string.IsNullOrWhiteSpace(commandData) ? "{}" : commandData,
                        commandType,
                        this.jsonSettings);

                    handler.Handle(command);

                    var result = new ObjectResult(null);

                    await context.WriteResultAsync(result);
                }
                catch (Exception exception)
                {
                    var response = WebApiErrorResponseBuilder.CreateErrorResponseOrNull(exception, context);

                    if (response != null)
                    {
                        await context.WriteResultAsync(response);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            else
            {
                await context.WriteResultAsync(new NotFoundObjectResult(commandName));
            }
        }
Ejemplo n.º 3
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            HttpRequest request = context.Request;

            string commandName = GetCommandName(request);

            if (this.commandTypes.ContainsKey(commandName))
            {
                Type commandType = this.commandTypes[commandName];

                string commandData = request.Body.ReadToEnd();

                Type handlerType = typeof(ICommandHandler <>).MakeGenericType(commandType);

                this.ApplyHeaders(request);

                dynamic handler = this.handlerFactory.Invoke(handlerType);

                try
                {
                    dynamic command = JsonConvert.DeserializeObject(commandData, commandType);//TODO - need to get the JSONSerializer settings object from somewhere

                    handler.Handle(command);

                    EmptyResult result = new EmptyResult();//TODO - might need to change to ObjectResult of some type see comment in HttpContextExtsions

                    await context.WriteResultAsync(result);
                }
                catch (Exception exception)
                {
                    var response = WebApiErrorResponseBuilder.CreateErrorResponseOrNull(exception, request);
                    if (response != null)
                    {
                        await context.WriteResultAsync(response);
                    }

                    throw;
                }
            }
            else
            {
                await context.WriteResultAsync(new NotFoundObjectResult(commandName));
            }
        }