Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Activiteiten kunnen communiceren met werkstroomtoepassingsextensies waarmee de host aanvullende functionaliteit kan bieden die niet expliciet is gemodelleerd in de werkstroom. In dit onderwerp wordt beschreven hoe u een extensie maakt en gebruikt om het aantal keren te tellen dat de activiteit wordt uitgevoerd.
Een activiteitsextensie gebruiken om uitvoeringen te tellen
Open Visual Studio 2010. Selecteer Nieuw, Project. Selecteer Werkstroom onder het knooppunt Visual C#. Selecteer De werkstroomconsoletoepassing in de lijst met sjablonen. Geef het project
Extensionseen naam. Klik op OK om het project aan te maken.Voeg een
usinginstructie toe aan het Program.cs-bestand voor de naamruimte System.Collections.Generic .using System.Collections.Generic;Maak in het bestand Program.cs een nieuwe klasse met de naam ExecutionCountExtension. Met de volgende code maakt u een werkstroomextensie waarmee exemplaar-id's worden bijgehouden wanneer de methode Register wordt aangeroepen.
// 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); } } }Maak een activiteit die de ExecutionCountExtension verbruikt. De volgende code definieert een activiteit die het ExecutionCountExtension-object uit de runtime ophaalt en de registratiemethode aanroept wanneer de activiteit wordt uitgevoerd.
// 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); } } }Implementeer de activiteit in de main-methode van het program.cs-bestand. De volgende code bevat methoden om twee verschillende werkstromen te genereren, elke werkstroom meerdere keren uit te voeren en de resulterende gegevens weer te geven die zijn opgenomen in de extensie.
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)); } }