public HttpResponseMessage Execute([FromBody] TCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            try
            {
                this.commandHandler.Handle(command);
            }
            catch (Exception ex)
            {
                var response = WebApiErrorResponseBuilder.CreateErrorResponse(ex, this.Request);

                if (response != null)
                {
                    return(response);
                }

                throw;
            }

            return(this.Request.CreateResponse <TCommand>(SuccessStatusCodeForThisCommandType, command));
        }
示例#2
0
        // Note: without [ModelBinder] or [FromUri], the query object won't get deserialized.
        public HttpResponseMessage Execute([ModelBinder] TQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            TResult result;

            try
            {
                result = this.processor.Execute(query);
            }
            catch (Exception ex)
            {
                var response = WebApiErrorResponseBuilder.CreateErrorResponse(ex, this.Request);

                if (response != null)
                {
                    return(response);
                }

                throw;
            }

            return(this.Request.CreateResponse <TResult>(SuccessStatusCodeForThisQueryType, result));
        }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                      CancellationToken cancellationToken)
        {
            string commandName = request.GetRouteData().Values["command"].ToString();

            if (request.Method != HttpMethod.Post)
            {
                return(request.CreateErrorResponse(HttpStatusCode.MethodNotAllowed,
                                                   "The requested resource does not support http method '" + request.Method + "'."));
            }

            if (!this.commandTypes.ContainsKey(commandName))
            {
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.NotFound, RequestMessage = request
                });
            }

            Type commandType = this.commandTypes[commandName];

            string commandData = await request.Content.ReadAsStringAsync();

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

            // GetDependencyScope() calls IDependencyResolver.BeginScope internally.
            request.GetDependencyScope();

            this.ApplyHeaders(request);

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

            try
            {
                dynamic command = DeserializeCommand(request, commandData, commandType);

                handler.Handle(command);

                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.OK, RequestMessage = request
                });
            }
            catch (Exception ex)
            {
                var response = WebApiErrorResponseBuilder.CreateErrorResponseOrNull(ex, request);

                if (response != null)
                {
                    return(response);
                }

                throw;
            }
        }
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                      CancellationToken cancellationToken)
        {
            string queryName = request.GetRouteData().Values["query"].ToString();

            if (!this.queryTypes.ContainsKey(queryName))
            {
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.NotFound, RequestMessage = request
                });
            }

            // 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 == HttpMethod.Get
                ? SerializationHelpers.ConvertQueryStringToJson(request.RequestUri.Query)
                : await request.Content.ReadAsStringAsync();

            QueryInfo info = this.queryTypes[queryName];

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

            // GetDependencyScope() calls IDependencyResolver.BeginScope internally.
            request.GetDependencyScope();

            this.ApplyHeaders(request);

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

            try
            {
                dynamic query = DeserializeQuery(request, queryData, info.QueryType);

                object result = handler.Handle(query);

                return(CreateResponse(result, info.ResultType, HttpStatusCode.OK, request));
            }
            catch (Exception ex)
            {
                var response = WebApiErrorResponseBuilder.CreateErrorResponseOrNull(ex, request);

                if (response != null)
                {
                    return(response);
                }

                throw;
            }
        }