Example #1
0
        private async Task <ActionResult> QueryAsync(string payload, Type type)
        {
            Ensure.NotNull(type, "type");

            object query = formatters.Query.Deserialize(type, payload);

            try
            {
                MethodInfo methodInfo = queryDispatcher.GetType().GetMethod(nameof(queryDispatcher.QueryAsync));
                if (methodInfo != null)
                {
                    methodInfo = methodInfo.MakeGenericMethod(type.GetInterfaces().First().GetGenericArguments().First());
                    Task  task = (Task)methodInfo.Invoke(queryDispatcher, new[] { query });
                    await task;

                    object       output       = task.GetType().GetProperty(nameof(Task <object> .Result)).GetValue(task);
                    ResponseType responseType = ResponseType.Composite;
                    if (output != null)
                    {
                        type = output.GetType();

                        if (plainTypes.Contains(type))
                        {
                            payload      = output.ToString();
                            responseType = ResponseType.Plain;
                        }
                        else
                        {
                            payload = formatters.Query.Serialize(output);
                        }
                    }
                    else
                    {
                        type    = type.GetInterfaces().First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IQuery <>)).GetGenericArguments()[0];
                        payload = null;
                        if (plainTypes.Contains(type))
                        {
                            responseType = ResponseType.Plain;
                        }
                    }

                    return(new ContentResult()
                    {
                        Content = json.Serialize(new Response()
                        {
                            Payload = payload,
                            Type = type.AssemblyQualifiedName,
                            ResponseType = responseType
                        }),
                        ContentType = "text/json"
                    });
                }

                return(NotFound());
            }
            catch (AggregateRootException e)
            {
                return(ProcessException(e));
            }
        }
Example #2
0
        public ActionResult Query([FromBody] Request request)
        {
            string payload = request.Payload;
            Type   type    = Type.GetType(request.Type);
            object query   = formatters.Query.Deserialize(type, payload);

            MethodInfo methodInfo = queryDispatcher.GetType().GetMethod(nameof(queryDispatcher.QueryAsync));

            if (methodInfo != null)
            {
                methodInfo = methodInfo.MakeGenericMethod(type.GetInterfaces().First().GetGenericArguments().First());
                Task task = (Task)methodInfo.Invoke(queryDispatcher, new[] { query });
                task.Wait();

                object output = task.GetType().GetProperty(nameof(Task <object> .Result)).GetValue(task);
                if (output != null)
                {
                    ResponseType responseType = ResponseType.Composite;
                    type = output.GetType();

                    if (output is string || output is int || output is decimal || output is bool)
                    {
                        payload      = output.ToString();
                        responseType = ResponseType.Plain;
                    }
                    else
                    {
                        payload = formatters.Query.Serialize(output);
                    }

                    HttpContext.Response.ContentType = "text/json";
                    return(Json(new Response()
                    {
                        Payload = payload,
                        Type = type.AssemblyQualifiedName,
                        ResponseType = responseType
                    }));
                }
            }

            return(NotFound());
        }