private BatchExecutionContext createBatchExecutionContext(Guid sessionId, object content)
        {
            var shareMessage   = new ShareMessageCollection();
            var responseObject = new ResponseObject(content, sessionId);

            shareMessage.Set <IResponseObject>(responseObject);

            BatchExecutionContext context = new BatchExecutionContext()
            {
                SessionId    = sessionId,
                ShareMessage = shareMessage
            };

            return(context);
        }
        public async Task <object> InvokeAsync(ContextInvoker context)
        {
            var actionToExecute   = context.ActionDescriptor;
            var executor          = MethodExecutor.Create(actionToExecute.ActionInfo, actionToExecute.BatchTypeInfo);
            var activatorInstance = _activator.CreateInstance <object>(context.RequestServices, actionToExecute.BatchTypeInfo.AsType());

            //Check Propertyes to activate
            if (actionToExecute.PropertyInfo != null)
            {
                await _propertyInvoker.invokeAsync(activatorInstance, context);
            }

            var batchExecutionContext = BatchExecutionContext.Create(context);

            //Execute attribute onExecuting

            foreach (var executionAttribute in  actionToExecute.ExecutionAttribute)
            {
                executionAttribute.onExecuting(batchExecutionContext);
            }

            var parameterBinding = new DefaultBatchInvokerParameterBinding(context.Parameters, actionToExecute.ActionInfo, _modelSerializer);
            var parameters       = parameterBinding.Bind();

            object result   = null;
            object response = null;

            try
            {
                result = executor.Execute(activatorInstance, parameters);

                if (actionToExecute.IsAsync)
                {
                    var   task = result as Task;
                    await task;

                    var responseType   = result.GetType();
                    var taskTType      = responseType.GetGenericArguments()[0];
                    var resultProperty = typeof(Task <>).MakeGenericType(taskTType).GetProperty("Result");
                    response = resultProperty.GetValue(task);
                }
                else
                {
                    response = result;
                }

                //Save response in ShareMessage
                IResponseObject responseObject = new ResponseObject(response, context.SessionId);
                context.ShareMessage.Set <IResponseObject>(responseObject);
            }catch (Exception ex)
            {
                IResponseObject responseObject = new ResponseObject(ex, context.SessionId);
                context.ShareMessage.Set <IResponseObject>(responseObject);
                await _sharpBatchTraking.AddExAsync(context.SessionId, ex);

                foreach (var item in actionToExecute.ExceptionAttribute)
                {
                    item.onExecuted(batchExecutionContext, ex);
                }
            }

            foreach (var executionAttribute in actionToExecute.ExecutionAttribute)
            {
                executionAttribute.onExecuted(batchExecutionContext);
            }

            var serializedResult = "";

            //If result not null i serialize it
            if (result != null)
            {
                serializedResult = _modelSerializer.Serialize(response);
            }
            return(serializedResult);
        }
 public override void onExecuted(BatchExecutionContext context, Exception ex)
 {
     context.Response.Body.Write(Encoding.UTF8.GetBytes("Error - "), 0, 5);
 }