Ejemplo n.º 1
0
        public static MethodExecutor Create(MethodInfo methodInfo, TypeInfo targetTypeInfo)
        {
            var executor = new MethodExecutor(methodInfo, targetTypeInfo);

            executor._executor = GetExecutor(methodInfo, targetTypeInfo);
            return(executor);
        }
Ejemplo n.º 2
0
        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);
        }