65.9K
CodeProject is changing. Read more.
Home

CallExternalMethod Activity in Windows Workflow Foundation

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.77/5 (9 votes)

Oct 10, 2011

CPOL

2 min read

viewsIcon

25212

downloadIcon

210

CallExternalMethod activity in Windows Workflow Foundation

Communication Services in Windows Workflow

What is ExternalDataExchangeService Class?

  • It comes under the namespace “System.Workflow.Activities”.
  • The instance of this class is added to the workflow runtime engine for local services communications to be enabled.

What is WorkflowRuntime Class?

  • It comes under the namespace “System.Workflow.Runtime”.
  • It is the service that help us to coordinate the execution of workflow and the services that use the workflow.

What is WorkflowInstance Class?

  • It comes under the namespace “System.Workflow.Runtime”.
  • It has some methods and properties that can be used to control the execution of a workflow instance.

CallExternalMethod Activity in Windows Workflow

It help us to make synchronous intercommunication between host and Workflow through the Local service with the help of Local Service Communication. This activity is used to send data from Workflow to Host application through the local service.

Example of CallExternalMethod

Pre-Requisites

  • .NET Framework 3.5 Framework
  • Visual Studio Team system 2008 or
  • .NET Framework 3.0 Framework
  • Visual Studio 2005 Professional Edition
  • Windows Workflow Foundation extensions for Visual Studio 2005

Step 1

Open Visual Studio and create a Sequential Workflow ConsoleApplication project as shown below:

Image1.png

Step 2

  • Add an Interface.
  • Add the namespace "System.Workflow.Activities".
  • Add method prototype.
  • Add ExternalDataExchange attribute in the interface.

Image2.png

Sample code given below for the above step:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Activities;

namespace CallExternalMethod
{
    [ExternalDataExchange]
    interface IBridge
    {
        void WorkFlowToHost(string msg);
    }
}

Step 3

  • Add a Class.
  • Implement the Interface.
  • Create a custom argument class that is derived from EventArgs.
  • Add event.

Image3.png

Sample code given below for the above step:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CallExternalMethod
{
    class Bridge:IBridge
    {
        public event EventHandler<SenddataEventArg> RaiseEventFromHost;
        public void WorkFlowToHost(string msg)
        {
            SenddataEventArg arg = new SenddataEventArg();
            arg.Response = msg;
            EventHandler<SenddataEventArg> Senddata = this.RaiseEventFromHost;
            if (Senddata != null)
            {
                Senddata(this, arg);
            }
        }
    }

    public class SenddataEventArg : EventArgs
    {
        string _response;
        public string Response
        {
            get { return _response; }
            set { _response = value; }
        }
    }
}	

Step 4

  • Open the designer window of the Workflow.
  • Drag and drop the "CallExternalMethod" activity.
  • Right click on the activity and select the properties.
  • Select the interface type.
  • Select the MethodName.
  • Declare a public property to the workflow class which is assigned to the "CallExternalMethod" class.

Image4.png

Image5.png

Image6.png

Image7.png

Sample code given below for the above step:
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

namespace CallExternalMethod
{
    public sealed partial class Workflow1 : SequentialWorkflowActivity
    {
        private string _message = "Hi, passing data from Workflow to host";
        public string Message
        {
            get
            {
                return _message;
            }
        }
        public Workflow1()
        {
            InitializeComponent();
        }
    }
}

Step 5

For program.cs:

  • Create the instance of "ExternalDataExchangeService" class in Program.cs file.
  • Add the Instance of "ExternalDataExchangeService" class to the WorkflowRuntime.
  • Create the instance of the Class i.e "Bridge.cs", that we have created.
  • Add the instance of Bridge class to the "ExternalDataExchangeService" class.
Sample code given below for the above step:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities;

namespace CallExternalMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                ExternalDataExchangeService externalService = 
				new ExternalDataExchangeService();
                workflowRuntime.AddService(externalService);
                Bridge comm = new Bridge();
                externalService.AddService(comm);

                AutoResetEvent waitHandle = new AutoResetEvent(false);
                workflowRuntime.WorkflowCompleted += 
		delegate(object sender, WorkflowCompletedEventArgs e) 
		{ waitHandle.Set(); };
                workflowRuntime.WorkflowTerminated += 
		delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    waitHandle.Set();
                };
                
                WorkflowInstance instance = 
		workflowRuntime.CreateWorkflow(typeof(CallExternalMethod.Workflow1));
                instance.Start();
                
                comm.RaiseEventFromHost += delegate(object sender, SenddataEventArg e)
                {
                    string val = e.Response;
                    Console.WriteLine(val);
                };

                Console.ReadLine();
                waitHandle.WaitOne();
            }
        }
    }
}

Step 6

Press F5 or click Run in order to run the project.

Image8.png

Reference

  • MSDN

Thanks