Beispiel #1
0
        public static Exception RetrieveCause(string details, DataConverter converter)
        {
            if (converter == null)
            {
                throw new ArgumentNullException("converter");
            }

            Exception cause = null;

            try
            {
                if (!string.IsNullOrWhiteSpace(details))
                {
                    cause = converter.Deserialize <Exception>(details);
                }
            }
            catch (Exception converterException)
            {
                cause = new TaskFailedExceptionDeserializationException(details, converterException);
            }

            return(cause);
        }
        public override async Task <string> RunAsync(TaskContext context, string input)
        {
            JArray jArray         = JArray.Parse(input);
            int    parameterCount = jArray.Count;

            ParameterInfo[] methodParameters = MethodInfo.GetParameters();
            if (methodParameters.Length < parameterCount)
            {
                throw new TaskFailureException(
                          "TaskActivity implementation cannot be invoked due to more than expected input parameters.  Signature mismatch.");
            }
            var inputParameters = new object[methodParameters.Length];

            for (int i = 0; i < methodParameters.Length; i++)
            {
                Type parameterType = methodParameters[i].ParameterType;
                if (i < parameterCount)
                {
                    JToken jToken = jArray[i];
                    var    jValue = jToken as JValue;
                    if (jValue != null)
                    {
                        inputParameters[i] = jValue.ToObject(parameterType);
                    }
                    else
                    {
                        string serializedValue = jToken.ToString();
                        inputParameters[i] = DataConverter.Deserialize(serializedValue, parameterType);
                    }
                }
                else
                {
                    if (methodParameters[i].HasDefaultValue)
                    {
                        inputParameters[i] = Type.Missing;
                    }
                    else
                    {
                        inputParameters[i] = parameterType.IsValueType ? Activator.CreateInstance(parameterType) : null;
                    }
                }
            }

            string serializedReturn;

            try
            {
                object invocationResult = InvokeActivity(inputParameters);
                if (invocationResult is Task)
                {
                    var invocationTask = invocationResult as Task;
                    if (MethodInfo.ReturnType.IsGenericType)
                    {
                        serializedReturn = DataConverter.Serialize(await((dynamic)invocationTask));
                    }
                    else
                    {
                        await invocationTask;
                        serializedReturn = string.Empty;
                    }
                }
                else
                {
                    serializedReturn = DataConverter.Serialize(invocationResult);
                }
            }
            catch (TargetInvocationException e)
            {
                Exception realException = e.InnerException ?? e;
                string    details       = Utils.SerializeCause(realException, DataConverter);
                throw new TaskFailureException(realException.Message, details);
            }
            catch (Exception e)
            {
                string details = Utils.SerializeCause(e, DataConverter);
                throw new TaskFailureException(e.Message, details);
            }

            return(serializedReturn);
        }