SIGN UP MEMBER LOGIN:    
ARTICLE

Passing and retrieving parameters from windows workflow

Posted by Sairam Articles | Workflow Foundation in C# June 09, 2008
Basically this article gives an idea about how to pass the parameters and getting output parameters from the workflow.
Reader Level:

Basically this article gives an idea about how to pass the parameters and getting output parameters from the workflow.

Firstly let me discuss about the work flow basics. There are two important classes to understand for executing any workflow.

Workflow runtime: This class represents workflow runtime execution environment.  We need to create an instance of work flow runtime engine to execute workflows. To execute any workflow first , we need to instantialte workflow runtime as below.

private WorkflowRuntime wfRunTime = new WorkflowRuntime();

Workflow instance:  Represents a workflow instance by using the specified workflow

WorkflowInstance wi;

Once we have runtime environment for executing the workflow, we need to create new workflow instance as below. There are five overloaded methods to create workflow instance from workflow runtime class .one of the method, it takes Dictionary collection which defines the passing parameters to the work flow and getting output parameters values from the workflow.

In this example, 'params' is declared as dictionary collection and passed to the create workflow instance method.

  var parms = new Dictionary<string, object>();

 

  wi = wfRunTime.CreateWorkflow(typeof(WorkflowEx), parms);

To retrieve the output result from workflow, workflowcompleted event of workflow runtime will be handled.

wfRunTime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(wfRunTime_WorkflowCompleted);

The WorkflowCompletedEventArgs property of output parameters. We can retrieve the output parameters from the completed workflow.

Dictionary<string, object> OutputParameters { get; }

 

Workflow is added to solution.


In this example, sequential workflow class is added to run this example and named as workflowEx.

If else activity is added to the workflow and based on the user choice, the code will executed.

There are two code activities

  1. output welcome message from the workflow
  2. Giving sum and difference  of the two numbers

All the properties i.e input parameters and output parameters will be declared in the workflow class

public partial class WorkflowEx : SequentialWorkflowActivity

          {

        private int _num1;

        private int _num2;

        private int _sum;

        private int _diff;

        private string _yourName;

        private string _outputMessage;

        private bool _choice =false;

 

        public bool Choice

        {

            set {  _choice =value; }

            get {

                return _choice;

            }

        }

 

        public string OutputMessage

        {

            get { return _outputMessage; }

            set

            {

                _outputMessage = value;

            }

           

        }

 

        public string YourName

        {

            set { _yourName = value; }

            get

            {

                return _yourName;

            }

        }

 

        public int Num1

        {

            set { _num1 = value; }

        }

 

        public int Num2

        {

            set { _num2 = value; }

        }

 

        public int Sum

        {

            get { return _sum; }

        }

 

        public int Diff

        {

            get { return _diff; }

        }

 

        private void AddNumberscodeActivity_ExecuteCode(object sender, EventArgs e)

        {

            _sum = _num1 + _num2;

            _diff = _num1 - _num2;

        }

 

        private void EvaluateCondition(object sender, ConditionalEventArgs e)

        {

            if (this.Choice)

            {

                e.Result = true;

            }

            else

            {

                e.Result = false;

            }

        }

        private void WelcomecodeActivity_ExecuteCode(object sender, EventArgs e)

        {

            OutputMessage = "hey " + YourName + " i am from work flow ";

        } 

        }

In this example, based on user choice, the work flow will be executed and returns the output. Once we create workflow, we need the instance of workflow instance to start the execution of the workflow instance and start the work flows instance to run.

As it was explained, create workflow takes the dictionary type of collection as parameter. Here, we are getting the values from windows form and passing the values to workflow.

Two choices are present on the windows form. Based on user choice, the workflow should return the welcome message or number addition.
     
choice = rdoString.Checked;
var parms = new Dictionary<string, object>();
parms.Add("Choice", choice);
wi = wfRunTime.CreateWorkflow(typeof(WorkflowEx), parms);
wi.Start();

If the user selects as a choice of welcome message , then pass the name to the workflow as below.

parms.Add("YourName", txtName.Text);

If the user selects as a choice of Number addition , then pass the input numbers to the workflow as below.

parms.Add("Num1", Int32.Parse(txtFstNum.Text));

parms.Add("Num2", Int32.Parse(txtScdNum.Text));

To capture the outparamters from the workflow, workflow completed event will be handled

wfRunTime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(wfRunTime_WorkflowCompleted);

Workflowcompleted event args is having propert of out paramters which contains the values of output from workflow.

Getting the  welcome message from the workflow:

string opMsge = (string)e.OutputParameters["OutputMessage"];

 

 

Getting the  sum and differnce result from the workflow:

sum = Convert.ToInt32(e.OutputParameters["Sum"]);

diff = Convert.ToInt32(e.OutputParameters["Diff"]);

Login to add your contents and source code to this article
Article Extensions
Contents added by sateesh gunnam on Mar 16, 2010
Contents added by philip on Jul 30, 2009
that's great, thanks.

do you know how to get data out of the workflowinstance on other events? specifically, when i load from

                  StateMachineWorkflowInstance smwi = new StateMachineWorkflowInstance(wr, wfGuid);
                  // This is empty. Why?? Where is this data that I populated so lovingly?
                  OrderRequest request = ((SupplyFulfillment) smwi.StateMachineWorkflow).Request;

then it's empty. That would be fine if my event for WorkflowLoaded returned something meaningful...but it just returns       void OnWorkflowLoaded(object sender, WorkflowEventArgs e)


thanks
share this article :
post comment
 

great, thanks

Posted by Loris Cabrini May 05, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor