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 activitiesWebServiceInput, WebServiceOutput, and WebServiceFaultthat 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 Else Return False End If End Function
Build the web service, then close the project. To test the web service, enter:
http:// localhost/SimpleWebService/Service.asmx
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:
http://localhost/SimpleWebService/Service.asmx.
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.
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.
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.
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#.