Export (0) Print
Expand All

Using Activity Extensions

.NET Framework 4.6 and 4.5

Activities can interact with workflow application extensions that allow the host to provide additional functionality that is not explicitly modeled in the workflow. This topic describes how to create and use an extension to count the number of times the activity executes.

To use an activity extension to count executions

  1. Open Visual Studio 2010. Select New, Project. Under the Visual C# node, select Workflow. Select Workflow Console Application from the list of templates. Name the project Extensions. Click OK to create the project.

  2. Add a using statement in the Program.cs file for the System.Collections.Generic namespace.

    using System.Collections.Generic;
    
    
  3. In the Program.cs file, create a new class named ExecutionCountExtension. The following code creates a workflow extension that tracks instance IDs when its Register method is called.

    // This extension collects a list of workflow Ids
    public class ExecutionCountExtension
    {
        IList<Guid> instances = new List<Guid>();
    
        public int ExecutionCount
        {
            get
            {
                return this.instances.Count;
            }
        }
    
        public IEnumerable<Guid> InstanceIds
        {
            get
            {
                return this.instances;
            }
        }
    
        public void Register(Guid activityInstanceId)
        {
            if (!this.instances.Contains<Guid>(activityInstanceId))
            {
                instances.Add(activityInstanceId);
            }
        }
    }
    
    
    
  4. Create an activity that consumes the ExecutionCountExtension. The following code defines an activity that retrieves the ExecutionCountExtension object from the runtime and calls its Register method when the activity executes.

    // Activity that consumes an extension provided by the host. If the extension is available
    // in the context, it will invoke (in this case, registers the Id of the executing workflow)
    public class MyActivity: CodeActivity
    {
        protected override void Execute(CodeActivityContext context)
        {
            ExecutionCountExtension ext = context.GetExtension<ExecutionCountExtension>();
            if (ext != null)
            {
                ext.Register(context.WorkflowInstanceId);                       
            }
    
        }
    }
    
    
  5. Implement the activity in the Main method of the program.cs file. The following code contains methods to generate two different workflows, execute each workflow several times, and display the resulting data that is contained in the extension.

    class Program
    {
        // Creates a workflow that uses the activity that consumes the extension
        static Activity CreateWorkflow1()
        {
            return new Sequence
            {
                Activities =
                {
                    new MyActivity()
                }
            };
        }
    
        // Creates a workflow that uses two instances of the activity that consumes the extension
        static Activity CreateWorkflow2()
        {
            return new Sequence
            {
                Activities =
                {
                    new MyActivity(),
                    new MyActivity()
                }
            };
        }
    
        static void Main(string[] args)
        {
            // create the extension 
            ExecutionCountExtension executionCountExt = new ExecutionCountExtension();
    
            // configure the first invoker and execute 3 times
            WorkflowInvoker invoker = new WorkflowInvoker(CreateWorkflow1());
            invoker.Extensions.Add(executionCountExt);                        
            invoker.Invoke();
            invoker.Invoke();
            invoker.Invoke();
    
            // configure the second invoker and execute 2 times
            WorkflowInvoker invoker2 = new WorkflowInvoker(CreateWorkflow2());
            invoker2.Extensions.Add(executionCountExt);
            invoker2.Invoke();
            invoker2.Invoke();
    
            // show the data in the extension
            Console.WriteLine("Executed {0} times", executionCountExt.ExecutionCount);
            executionCountExt.InstanceIds.ToList().ForEach(i => Console.WriteLine("...{0}", i));
        }
    }
    




Build Date:

2012-08-02
Show:
© 2015 Microsoft