The CallExternalMethod Activity allows us to make synchronous communication between Workflow and the Host through the Local Service using Local Service Communication.

Local Service Communication allows workflows to communicate with an external service that resides within the Host.

Using this activity involves 4 phases:

1.    Contract interface creation;

2.    Workflow CallExternalMethod activity configuration with the contract interface, the parameter(s) and return properties (if applies);

3.    Contract implementation creation;

4.    Using ExternalDataExchangeService class at the Host, to register the Contract implementation

We can optionally set the input properties and read the return data.

Example:

In this example I'm going to show the execution of a simple method that returns the 2 input parameters concatenation.

1.    Contract interface creation

    In the contract interface you have only to create the interface contract definition used by the Workflow.

    You're going to create an Interface named "IContract", which defines the Concatenate method.

    All interfaces must have the ExternalDataExchange attribute defined that marks the interface as a local service interface.

       [ExternalDataExchange]
    public interface IContract 
    {

        string Concatenate(string parameter1, string parameter2);
       }

2.    Workflow CallExternalMethodActivity configuration

    Add the CallExternalMethodActivity to the Workflow.

    The Workflow will appear like this:

    Then you will create three properties that correspond to the two input parameters to concatenate and the return concatenation.

    public string Parameter1ToConcat { get; set; }

    public string Parameter2ToConcat { get; set; }

    public string ConcatenateReturn { get; set; }

    

    Configure the CallExternalMethodActivity properties like this:

InterfaceType

IContract interface

MethodName

Concatenate

Parameter1 & parameter2

Parameter1ToConcat & Parameter2ToConcat properties

(ReturnValue)

ConcatenateReturn property

 

3.    Contract implementation creation

    At the contract implementation creation, you have to create a class that implements IContract defined at the first step.

  public class ContractImplementation : IContract
  { 

      public string Concatenate(string parameter1, string parameter2)                      
      { 

          return parameter1 + parameter2; 
           }            
   }

4.    Host configuration

    At the Host, the contract implementation is registered by using the ExternalDataExchangeService class.

    The input properties can be set by using the second parameter from the CreateWorkflow method.

    The return parameter can be retrieved using the OutputParameters property from the WorkflowCompletedEventArgs class, at the WorkflowCompleted event.

using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())

  …  
  workflowRuntime.WorkflowCompleted += delegate(
          object sender, WorkflowCompletedEventArgs e) 
    {
      // this is where we can get the return value 
      Console.Write((string)e.OutputParameters["ConcatenateReturn"]); 
      waitHandle.Set();
    } 
  … 
  ExternalDataExchangeService externalDataExchangeService = new ExternalDataExchangeService();  

  // add the external data exchange service to the runtime 
  workflowRuntime.AddService(externalDataExchangeService); 

  // add the external method implementation to the local services 
  // this is where we add the object(that implements IContract)
  // that is going to be invoked by the CallExternalMethodActivity 
  externalDataExchangeService.AddService(new ContractProject.ContractImplementation()); 

  Dictionary<string, object> parameters = new Dictionary<string, object>(); 

  // add both paramenters  
  parameters.Add("Parameter1ToConcat", "1"); 
  parameters.Add("Parameter1ToConcat", "2"); 

  WorkflowInstance instance = workflowRuntime.CreateWorkflow(
              typeof(WorkflowConsoleApplication1.Workflow1), parameters); 
  instance.Start(); 
  waitHandle.WaitOne();

LEAVE A REPLY

Please enter your comment!
Please enter your name here