July 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Activity Binding in Windows Workflow Foundation

  • January 31, 2007
  • By Rachel Wireman
  • Send Email »
  • More Articles »

Putting It All Together

Now, it is time to see all your hard work in action. You can modify the Main method in Program.cs in your console application to prompt the user for a first and last name, like so:

Console.Write("First Name:  ");
string firstName = Console.ReadLine();
Console.Write("Last Name:  ");
string lastName = Console.ReadLine();

Now, you must add the user input to a dictionary object that you will pass to the CreateWorkflow method. (For more information regarding dictionary objects, please see MSDN.) The CreateWorkflow method is overloaded to include a signature that allows the passing of a dictionary object as a parameter to be made available to the workflow. The names of the dictionary entries correspond to the names of the workflow's properties. Note the code in the red boxes:

static void Main(string[] args)
{
    Console.Write("First Name:  ");
    string firstName = Console.ReadLine();
    Console.Write("Last Name:  ");
    string lastName = Console.ReadLine();
    Dictionary<String, Object> parameters = new Dictionary<string, object>();
    parameters.Add("WorkflowFirstName", firstName);
    parameters.Add("WorkflowLastName", lastName);
    using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
    {
        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(MyWorkflowConsoleApplication.MyWorkflow), parameters);
        instance.Start();
        waitHandle.WaitOne();
    }
    Console.ReadLine();
}

All that you have left to do now is run the application (see Figure 4).



Click here for a larger image.

Figure 4: Run GreetingActivity Application

Binding Activity Properties to Other Activity Properties

Activity binding is not limited to activity-property-to-workflow-property binding. You also can bind the dependency properties of an activity to the properties of another activity. As an example, I created another activity called LogActivity in the MyActivityLibrary project and added an instance of the activity to the workflow. This activity has two dependency properties, LogFirstName and LogLastName, which I bound to the GreetingActivity's FirstName and LastName properties in the workflow designer (see Figure 5).

Figure 5: Binding LogFirstName and LogLastName to the GreetingActivity's FirstName and LastName

I then overrode the LogActivity's Execute method, like so:

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
    Console.WriteLine(LogFirstName + " " + LogLastName + " logged in at " + DateTime.Now.ToString());
    return base.Execute(executionContext);
}

Those are the only changes I needed to bind the new activity's properties to the GreetingActivity's properties, which, in turn, are those retrieved by the console application. Figure 6 shows the result.



Click here for a larger image.

Figure 6: Result of LogActivity

Note: You can achieve the same result in Figure 6 by binding the LogActivity's properties directly to the properties exposed in the workflow, but this example demonstrates only how you can bind an activity to another activity.

Create Custom Activities

Dependency properties and activity binding allow a developer to create custom activities. These activities offer such seamless reusability and ease of implementation that they become indistinguishable from the out-of-the-box activities provided by Windows Workflow Foundation. In addition to binding an activity's dependency properties to workflow properties, the dependency properties also can be bound to other activities' properties. Activity binding, therefore, permits behind-the-scenes propagation of values, allowing activities and workflows to share information.

Activity binding obviously can provide value beyond the simple example presented in this article. It can add functionality to your applications by connecting the components of your workflow and by allowing top-level application code to communicate down to the activity level of a workflow.

Download the Code

Download the activity binding example here.

About the Author

Rachel Wireman (MCTS) is a developer specializing in Windows-based solutions at Crowe Chizek in Oak Brook, Illinois. Reach her at rwireman@crowechizek.com.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel