Пример #1
0
        /// <summary>
        /// Runs when the activity is run by the workflow.
        /// </summary>
        /// <param name="context">The run state.</param>
        /// <param name="inputs">The inputs.</param>
        public void OnRunNow(IRunState context, ActivityInputs inputs)
        {
            var startedKey    = GetArgumentKey(StartedArgumentAlias);
            var listKey       = GetArgumentKey(ListArgumentAlias);
            var appIdKey      = GetArgumentKey(ApplicationIdArgumentAlias);
            var appVersionKey = GetArgumentKey(ApplicationVersionArgumentAlias);

            var ids     = new List <Guid>();
            var started = context.GetArgValue <bool?>(ActivityInstance, startedKey) ?? false;

            if (!started)
            {
                // set that the activity has started
                context.SetArgValue(ActivityInstance, startedKey, true);

                // retrieve the product
                var productSku = GetArgumentValue <string>(inputs, ProductSkuArgumentAlias);

                var product = MarketplaceService.GetProduct(productSku);
                if (product == null)
                {
                    throw new WorkflowRunException("Product {0} was not found.", productSku);
                }

                // process the included apps and app versions and sort their ids
                ids.AddRange(GetSortedApplicationIds(product));
            }
            else
            {
                // retrieve the current state of the ordered list from the context
                var list = context.GetArgValue <string>(ActivityInstance, listKey);
                if (!string.IsNullOrEmpty(list))
                {
                    ids.AddRange(list.Split(',').Select(Guid.Parse));
                }
            }

            // loop over the next id on the list
            var current = ids.FirstOrDefault();

            if (current != default(Guid))
            {
                // set the application id and any specific version info on the output
                var variables = GetAppVariables(context, current);
                if (variables.Item1 != Guid.Empty)
                {
                    context.SetArgValue(ActivityInstance, appIdKey, variables.Item1);
                    context.SetArgValue(ActivityInstance, appVersionKey, variables.Item2);
                }

                // remove this id from the list and store it again
                ids = ids.Skip(1).ToList();

                var list = string.Join(",", ids);

                context.SetArgValue(ActivityInstance, listKey, list);
                context.ExitPointId = new EntityRef(LoopExitPointAlias);
            }
            else
            {
                // we have finished
                context.SetArgValue(ActivityInstance, startedKey, false);
                context.SetArgValue(ActivityInstance, listKey, null);
                context.ExitPointId = new EntityRef(FinishedExitPointAlias);
            }
        }