Udostępnij za pośrednictwem


WF: How to add your own task in the workflow runtime engine batching mechanism ?

 

The goal of this post is to describe the mechanism to create and add your own service in the workflow runtime engine.

Situation: You would like that each time the SqlWorkflowPersistenceService is called by the workflow runtime, it call your own service, in a transactional scope (if your service or the persistence service failed, a rollback is apply).

1: You have to create your service:

Create a class (in a separate project) wich implements IPendingWork. Add reference of this project in the host and workflow project.

namespace TransactionalService
{
public class TransactionalService : IPendingWork
{
   void IPendingWork.Commit(System.Transactions.Transaction transaction, ICollection items)
   {
      foreach (MyExternalDataEventArgs request in items)
      {
         Console.WriteLine(request.Commande.Nom);
      }
   }

   bool IPendingWork.MustCommit(ICollection items)
   {
      return true;
   }

   void IPendingWork.Complete(bool succeeded, ICollection items)
   {
      Console.WriteLine(succeeded);
   }
  }
}

2: Add your service in the workflowruntime

In your Host:


workflowRuntime.AddService(new TransactionalService.TransactionalService());
workflowRuntime.StartRuntime();

3: add your pending work items to the current work batch

In your Workflow:

TransactionalService.TransactionalService service = new TransactionalService.TransactionalService(); //Or by the getService()

WorkflowEnvironment.WorkBatch.Add(service, MyArgs);

When the runtime engine batching will be executed, he will call all services which implements IPendingWork (SqlTrackingService, SqlWorkflowPersistenceService and you service):
1: MustCommit (check if you want the method Commit will be called) ,
2: if yes, he will call Commit method,
3: The complete method allow us to know the state of the process.