Dela via


Använda aktivitetstillägg

Aktiviteter kan interagera med programtillägg för arbetsflöden som gör att värden kan tillhandahålla ytterligare funktioner som inte uttryckligen modelleras i arbetsflödet. Det här avsnittet beskriver hur du skapar och använder ett tillägg för att räkna antalet gånger aktiviteten körs.

Så här använder du ett aktivitetstillägg för att räkna körningar

  1. Öppna Visual Studio 2010. Välj Nytt, Projekt. Under noden Visual C# väljer du Arbetsflöde. Välj Arbetsflödeskonsolprogram i listan över mallar. Ge projektet namnet Extensions. Klicka på OK för att skapa projektet.

  2. Lägg till ett using direktiv i filen Program.cs för namnområdet System.Collections.Generic .

    using System.Collections.Generic;
    
  3. I filen Program.cs skapar du en ny klass med namnet ExecutionCountExtension. Följande kod skapar ett arbetsflödestillägg som spårar instans-ID när dess Register-metod anropas.

    // 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. Skapa en aktivitet som använder ExecutionCountExtension. Följande kod definierar en aktivitet som hämtar ExecutionCountExtension-objektet från körningen och anropar dess Register-metod när aktiviteten körs.

    // 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. Implementera aktiviteten i main-metoden för program.cs-filen. Följande kod innehåller metoder för att generera två olika arbetsflöden, köra varje arbetsflöde flera gånger och visa resulterande data som finns i tillägget.

    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));
        }
    }