Channels ▼


Windows Workflow Foundation and Web Services

Source Code Accompanies This Article. Download It Now.

Brian is a Microsoft Certified Professional and Microsoft Certified Application Developer for .NET. Brian is also the author of Foundations of WF: An Introduction to Windows Workflow Foundation (Apress, 2007) on which this article is based.

Windows Workflow Foundation (WF) is the programming model and tools for building "workflow-enabled" (a set of related activities representing specific business logic) Windows applications. The ultimate goal of WF is to simplify development of business processes in .NET. One of the activities that Windows Workflow provides is an activity for working with web services, along with several activities that let your workflow become a web service. In this article, I introduce the InvokeWebService activity, which lets you work with existing web services. I also introduce three other activities—WebServiceInput, WebServiceOutput, and WebServiceFault—that let you create your workflow as a web service.

InvokeWebService Activity

Windows Workflow Foundation supports three workflows: Sequential, State Machine, and Data-Driven. You can use the InvokeWebService activity in both the Sequential and State Machine workflows. It's similar to using a web service in any other .NET application. First, you must reference the web service, then you can access it. To illustrate, I present a simple web service that returns a value of True. Although impractical in the real world, it gives a good example of how the interaction works.

The first step in this example is to create a simple web service. The completed web service, SimpleWebService, is available electronically; see "Resource Center," page 5. However, if you wish to build the web service, here are the steps to do it. (If you aren't familiar with web services, you can find an introduction to web services in Chapter 10 of my book Beginning Object-Oriented ASP.NET 2.0 with VB.NET; Apress, 2005.)

Create a new web site, choose ASP.NET Web Service, and call the service SimpleWebService. When the web service is created, open the Service.vb file if it isn't already open. Replace the HelloWorld default web method with the following:

Public Function IsUser(ByVal UserName As
                     String) As Boolean
   If UserName = "Brian" Then
     Return True
     Return False
   End    If
End Function

Build the web service, then close the project. To test the web service, enter:


in your web browser. The Service web service default page appears, listing the web methods (IsUser). Click the IsUser link, enter Brian as the UserName parameter, and click Invoke. An XML document appears in a new browser window; it says True. Close the XML document window, enter Me as the UserName parameter, and click Invoke. This time the XML document contains False, showing that the web service is working correctly.

Create a new VB Sequential Workflow Console Application called "VBWebServiceSequentialConsole." Drag an InvokeWebService activity onto the Workflow Designer. As soon as the activity is added to the workflow, the Add Web Reference window appears. Click the Web Services on the Local Machine link. A list of the web services on the local machine is listed here. Find the link with the URL listed as:


The Services column lists Service. Click the URL. The Service default page appears as it did in the browser previously. Change the Web Reference Name to SimpleWebService and click the Add Reference button. This is the same as adding a web reference to a VB or C# application.

After the Add Web Reference window closes, click the InvokeWebService activity and view the properties. Change the name to InvokeSimpleWebService. Click the MethodName property and choose IsUser from the drop-down. Each InvokeWebService activity lets you interact with one web method exposed by the web service. Once you choose IsUser, the (ReturnValue) and UserName properties appear under Parameters, as in Figure 1. These two properties let you provide values or variable names that contain the values to send to the web method when it's called.

[Click image to view at full size]

Figure 1: InvokeSimpleWebService properties.

View the code for the workflow to add the necessary variables. Add the following code within the workflow class declaration:

Public LoginName As String = "Brian"
Public ValueReturned As Boolean = False

Return to the Workflow Designer, click the InvokeSimpleWebService activity, and view the properties. Click the ellipse next to the UserName property. This opens the Bind to Property window. Choose LoginName from the tree under Workflow1, as in Figure 2.

[Click image to view at full size]

Figure 2: Choose LoginName as an existing member to bind to.

Right-click the (ReturnValue) property name and choose Commands if it's not already selected. You should now see a Promote Bindable Properties link at the bottom of the Properties window. Click the Promote Bindable Properties link. A default value is placed in the Return Value property. Click the ellipse next to the Return Value property; this brings up the Bind Property window again. Choose ValueReturned from the list under Workflow1; see Figure 3. The Parameters properties for InvokeSimpleWebService look like Figure 4.

[Click image to view at full size]

Figure 3: Select the ValueReturned property.

[Click image to view at full size]

Figure 4: Parameters property with ReturnValue and UserName set.

Drag a Code activity onto the Workflow Designer before the InvokeSimpleWebService activity. Call this Code activity BeforeInvokeCode, generate Handlers for the activity, and add the following code to the sub generated:

MsgBox("Before Invoke: " & ValueReturned)

This displays a message box with the value of blnReturn prior to the web service being called. Next, drag a Code activity onto the Workflow Designer after the InvokeSimpleWebService. Call this Code activity AfterInvokeCode, generate Handlers for the activity, and add the following code to the sub generated:

MsgBox("After Invoke: " & ValueReturned)

Execute the workflow, and you'll see the Before box appear with False and the After box appear with True. This shows that the workflow called out to the web service providing the variable LoginName as a parameter, and received the parameter ValueReturned back.

There's little change between the Sequential workflow and the State Machine workflow. The only difference is that the InvokeWebService activity must be placed inside an EventDriven activity, like any other activity within a State Machine workflow. Also, the logic behind the code is the same between VB and C#.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.