public CommandConverter(
     RestApplication application,
     IObjectFactory objectFactory,
     IProcessingEngine processingEngine,
     IWireSerialization serialization)
 {
     this.Application      = application;
     this.ObjectFactory    = objectFactory;
     this.ProcessingEngine = processingEngine;
     this.Serialization    = serialization;
 }
        public Task PassThrough <TCommand, TArgument>(
            TArgument argument,
            string accept,
            HttpContext contex,
            AdditionalCommand[] additionalCommands,
            string customContentType = null)
        {
            var          request  = contex.Request;
            var          response = contex.Response;
            var          start    = Stopwatch.GetTimestamp();
            var          engine   = ProcessingEngine;
            StringValues headers;

            if (request.Headers.TryGetValue("x-revenj-session-id", out headers) && headers.Count == 1)
            {
                var sessionID = headers[0];
                var scope     = ObjectFactory.FindScope(sessionID);
                if (scope == null)
                {
                    return(response.WriteError("Unknown session: " + sessionID, HttpStatusCode.BadRequest));
                }
                engine = scope.Resolve <IProcessingEngine>();
            }
            var commands = new ObjectCommandDescription[1 + (additionalCommands != null ? additionalCommands.Length : 0)];

            commands[0] = new ObjectCommandDescription {
                Data = argument, CommandType = typeof(TCommand)
            };
            for (int i = 1; i < commands.Length; i++)
            {
                var ac = additionalCommands[i - 1];
                commands[i] = new ObjectCommandDescription {
                    RequestID = ac.ToHeader, CommandType = ac.CommandType, Data = ac.Argument
                };
            }
            var task = RestApplication.ExecuteCommands <object>(engine, Serialization, commands, contex, accept);

            return(task.ContinueWith(t =>
            {
                if (t.IsCompletedSuccessfully)
                {
                    var elapsed = (decimal)(Stopwatch.GetTimestamp() - start) / TimeSpan.TicksPerMillisecond;
                    if (customContentType != null)
                    {
                        response.ContentType = customContentType;
                    }
                    response.Headers.Add("X-Duration", elapsed.ToString(CultureInfo.InvariantCulture));
                    var stream = t.Result;
                    var cms = stream as ChunkedMemoryStream;
                    if (cms != null)
                    {
                        cms.CopyTo(response.Body);
                        cms.Dispose();
                    }
                    else if (stream != null)
                    {
                        stream.CopyTo(response.Body);
                        stream.Dispose();
                    }
                }
            }));
        }