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

            if (converter == null)
            {
                throw new ArgumentNullException("converter");
            }

            string details = null;

            try
            {
                details = converter.Serialize(originalException);
            }
            catch
            {
                // Cannot serialize exception, throw original exception
                throw originalException;
            }

            return(details);
        }
        /// <summary>
        ///     Create a new orchestration of the specified name and version
        /// </summary>
        /// <param name="name">Name of the orchestration as specified by the ObjectCreator</param>
        /// <param name="version">Name of the orchestration as specified by the ObjectCreator</param>
        /// <param name="instanceId">Instance id for the orchestration to be created, must be unique across the Task Hub</param>
        /// <param name="input">Input parameter to the specified TaskOrchestration</param>
        /// <param name="tags">Dictionary of key/value tags associated with this instance</param>
        /// <returns>OrchestrationInstance that represents the orchestration that was created</returns>
        public async Task <OrchestrationInstance> CreateOrchestrationInstanceAsync(string name, string version,
                                                                                   string instanceId,
                                                                                   object input, IDictionary <string, string> tags)
        {
            if (string.IsNullOrWhiteSpace(instanceId))
            {
                instanceId = Guid.NewGuid().ToString("N");
            }

            var orchestrationInstance = new OrchestrationInstance
            {
                InstanceId  = instanceId,
                ExecutionId = Guid.NewGuid().ToString("N"),
            };

            string serializedInput = defaultConverter.Serialize(input);
            string serializedtags  = tags != null?defaultConverter.Serialize(tags) : null;

            var startedEvent = new ExecutionStartedEvent(-1, serializedInput)
            {
                Tags    = serializedtags,
                Name    = name,
                Version = version,
                OrchestrationInstance = orchestrationInstance
            };

            var taskMessage = new TaskMessage
            {
                OrchestrationInstance = orchestrationInstance,
                Event = startedEvent
            };

            BrokeredMessage brokeredMessage = Utils.GetBrokeredMessageFromObject(taskMessage,
                                                                                 settings.MessageCompressionSettings);

            brokeredMessage.SessionId = instanceId;

            MessageSender sender =
                await messagingFactory.CreateMessageSenderAsync(orchestratorEntityName).ConfigureAwait(false);

            await sender.SendAsync(brokeredMessage).ConfigureAwait(false);

            await sender.CloseAsync().ConfigureAwait(false);

            return(orchestrationInstance);
        }
        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);
        }