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; } }