Search This Blog

2009-04-26

Tutorial 2:Receiving Data into the Workflow using Parameters

Receiving Data into the Workflow using Parameters:
In this exercise, you will modify the workflow we created in Tutorial 1 to receive data into our workflow when it is started. You will also modify the code in our Code activity’s handler to display the input values. Finally, you will change our startup project to use a Windows Forms (WinForms) host that will allow us to enter the input values and create an instance of our workflow.
Note: There are two general approaches for receiving data into a workflow when it is started. They are Parameters and Events. With parameters, a list of parameter names and types are defined with the workflow. These parameter values are passed in by a host when it starts a new instance of the workflow type. With events, workflow authors add an activity that receives an event and data associated with the event. Events are generally specific to the host and custom activities that have been designed to handle the event.
Task 1 – Define parameters for the workflow
1. In Visual Studio 2005 with the HelloWorldWorkflow project open, double click on the Workflow1.xoml file in Solution Explorer to open the Visual Studio workflow designer. 2. Right-click the workflow background in the Visual Studio workflow designer and click View Code, or select the View Code menu item. 3. Inside the Workflow1 class, type “prop” and select prop from the intelli-sense menu, to insert the snippet hit tab.

4. Fill in each of the highlighted areas in the snippet to create a string property called FirstName with a private member called firstName as shown below.


5. Use the same technique to include another string property called LastName with the private member lastName.
Task 2 – Modify the Code Activity
1. In this exercise we will use a Windows Forms application as the workflow host. Since we want to display a message box from the workflow application, we need to add a reference to the assembly for Windows Forms. Select the Project Add Reference menu item.
2. On the .NET tab in the Add Reference dialog, select System.Windows.Forms from the list of assemblies and click the OK button.

3.Right click on the Workflow1.xoml.cs file in Solution Explorer and select the View Code menu item from the context menu. Navigate to the method codeActivity1_CodeHandler in the 1. Workflow1.xoml.cs source file. This method is the code handler for the code activity in the workflow.
4. Modify the code in the codeActivity1_CodeHandler method to get the parameter values and display a Message Box with the FirstName and LastName values:
private void codeActivity1_CodeHandler(object sender, EventArgs e)
{
System.Windows.Forms.MessageBox.Show(
"Hello world: " + firstName + " " + lastName);
}
Task 3 – Execute the modified workflow
Now we’re ready to test the modified workflow. Instead of using a console host application as we did in Exercise 1, in this exercise we’re going to add a new Windows Forms application and some pre-written code as a host for our workflow.

1. In Visual Studio 2005, with the HelloWorldWorkflow solution open, add a new project, WinFormTestHost to the solution by selecting the File Add New Project menu item as shown below:


3.Design the Windows forms as follows

Step 5. Write the following code in the windows application
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Workflow.ComponentModel;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;

namespace WinFormTestHost
{
public partial class Form1 : Form
{
private WorkflowRuntime wr;
private string workflowAssembly = "";
private string workflowTypeName = "";
public Form1()
{
InitializeComponent();
workflowAssembly = "HelloWorldWorkflow";
workflowTypeName = "HelloWorldWorkflow.Workflow1";
}
private void btnStartWorkflow_Click(object sender, EventArgs e)
{
if (wr == null)
{
wr = new WorkflowRuntime();
wr.StartRuntime();
}
Dictionary parameters = new Dictionary(); parameters.Add("FirstName", txtFirstName.Text);
parameters.Add("LastName", txtLastName.Text);
WorkflowInstance instance = wr.CreateWorkflow(typeof(HelloWorldWorkflow.Workflow1), parameters);
instance.Start();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (wr != null)
{ if (wr.IsStarted)
{ wr.StopRuntime();
}
}
}
}
}
4. Using the Solution Explorer tool window, right click on the WinFormTestHost project and select Set as Startup Project from the context menu.


5. Now we need to make sure that the WinFormTestHost application can use the new workflow. Add a reference to the HelloWorldWorkflow project by right clicking on the References folder for the WinFormTestHost in Solution Explorer and selecting Add Reference from the context menu.
6. In the Add Reference dialog window, select the Projects tab.
7. Select the HelloWorldWorkflow project from the list and press the OK button to close the Add Reference dialog window and add the new reference.

8. We also need to ensure that the WinFormTestHost has references to the Windows Workflow Foundation system assemblies. Add these references by right clicking on the References folder for the WinFormTestHost in Solution Explorer and selecting Add Reference from the context menu
9. In the Add Reference dialog window, select the .NET tab, scroll down and select the entries shown below and click OK.

10. Right click on the Workflow1.xoml.cs file in Solution Explorer and select the View Code menu item from the context menu to open the code-beside class for the workflow.
11. Set a breakpoint in the codeActivity1_CodeHandler method.
12. Compile and run the solution under the Visual Studio debugger by pressing F5 or selecting the Debug Start Debugging menu command.
13. The WinFormTestHost will start and display a window containing fields for entering a First and Last Name.
14. Enter values in the First name and Last name fields
15. Click the Start Workflow button to instantiate a new instance of the HelloWorldWorkflow.Workflow1 and pass in the First name and Last name values as parameters.(Images are same as Task3-Step 3)
16. Visual Studio 2005 will then break into debug mode for the codeActivity1_CodeHandler method. Continue execution of the workflow by selecting the Debug Continue menu item.

17. When the code activity finishes executing you will see a message box with the first name and last name were passed as parameters to the workflow.
18. Close the WinFormTestHost application to stop the host and stop the debugger.

Reference:http://msdn.microsoft.com/workflow

1 comment:

Anonymous said...

Bah khub bhalo, ami sob bujhe gechi.........:)ARPANA