Click here to Skip to main content
Click here to Skip to main content

Tagged as

CallExternalMethod Activity in Windows Workflow Foundation

, 10 Oct 2011
Rate this:
Please Sign up or sign in to vote.
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

License

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

Share

About the Author

Pijush kumar Joardar
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMita Joardar2-Jul-13 4:05 
GeneralMy vote of 5 PinmvpKanasz Robert5-Nov-12 0:45 
GeneralMy vote of 5 PinmemberRanjit Chakraborti10-Oct-11 10:25 
GeneralMy vote of 5 PinmvpAbhishek Sur10-Oct-11 2:19 
GeneralMy vote of 5 Pinmemberdubey_n_c10-Oct-11 1:33 
GeneralMy vote of 4 Pinmemberbasak200710-Oct-11 1:18 
QuestionWindows Workflow Foundation Pinmemberbasak200710-Oct-11 1:16 
GeneralMy vote of 5 PinmemberMember 35076810-Oct-11 1:11 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 10 Oct 2011
Article Copyright 2011 by Pijush kumar Joardar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid