Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / C#

CallExternalMethod Activity in Windows Workflow Foundation

Rate me:
Please Sign up or sign in to vote.
4.77/5 (9 votes)
10 Oct 2011CPOL2 min read 24.7K   210   6   8
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:
C#
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:
C#
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:
C#
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:
C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mita Joardar2-Jul-13 4:05
Mita Joardar2-Jul-13 4:05 
GeneralMy vote of 5 Pin
Kanasz Robert5-Nov-12 0:45
professionalKanasz Robert5-Nov-12 0:45 
Good one.
GeneralMy vote of 5 Pin
Ranjit Chakraborti10-Oct-11 10:25
Ranjit Chakraborti10-Oct-11 10:25 
GeneralMy vote of 5 Pin
Abhishek Sur10-Oct-11 2:19
professionalAbhishek Sur10-Oct-11 2:19 
GeneralMy vote of 5 Pin
dubey_n_c10-Oct-11 1:33
dubey_n_c10-Oct-11 1:33 
GeneralMy vote of 4 Pin
basak200710-Oct-11 1:18
basak200710-Oct-11 1:18 
QuestionWindows Workflow Foundation Pin
basak200710-Oct-11 1:16
basak200710-Oct-11 1:16 
GeneralMy vote of 5 Pin
Mita Joardar10-Oct-11 1:11
Mita Joardar10-Oct-11 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.