Introduction
One of the key features of Windows Workflow is the excellent integration it has with Web
services. This is very important as most of the business processes now have a Web services foundation that powers
these business processes. Through this integration, you can now easily invoke a Web service from a workflow. In
addition, you can also programmatically expose a workflow as a Web service to the consumers with very little
effort. Windows Workflow accomplishes this through a bunch of Web service activities that abstract
the complexities associated with invoking and exposing Web services. This article will discuss the integration
between workflow and Web services through examples. Specifically this article will provide a simple example that
shows how to invoke a Web service from within a workflow. Finally, you will also see the steps involved in
exposing the workflow as a Web service by leveraging the integration between Visual Studio 2005 and
Windows Workflow.
Prerequisites
To follow along with the examples throughout this article series, you need the following installed on your
system:
- .NET Framework 3.0 - Because
Windows Workflow Foundation is built on top of
this, you need the .NET Framework 3.0 installed for runtime functionality. Windows Vista comes with the .NET 3.0
Framework out of the box as long as you select this option at the time of installing Windows Vista. If you are
running an operating system other than Windows Vista, you need to install the .NET Framework 3.0.
- Visual Studio 2005
(any edition) - Required for workflow development.
- Windows Workflow Foundation extensions for Visual Studio 2005 - Available for download from
Microsoft.
Invoking a Web Service from a Workflow
To begin, let us look at an example of invoking a simple Web service from a workflow. Let us start by creating
the Web service that will be invoked from the workflow.
Implementation of Web Service
Open Visual Studio 2005 and create a new Visual C# Web Service named HelloWorldService
in the folder http://localhost/MyProjects/DotNetSlackers. Once the Web
service project is created, add a new Web service class named HelloWorldService and modify its
code-behind to look as follows:
As you can see, the Web service is very simple and straightforward. It exposes a method named HelloWorld, which
returns a string back to the consumers. Now that you have created the Web service, let us write the code to
invoke the Web service from a work flow.
Invoking the Web Service
To invoke the Web service, you need to add an activity named InvokeWebService to the workflow and configure its
properties to point to the Web service. Using the InvokeWebService activity is simply a matter of dropping it
into your workflow designer. When you do that, Visual Studio helps you configure the activity by allowing you to
specify the Web service information so that it can retrieve the WSDL and create the proxy for you. For this
purpose, Visual Studio displays the familiar Add Web Reference dialog box that you typically use to create a
proxy in Visual Studio. If you want to create the proxy yourself, simply cancel the Add Web Reference dialog box
and manually add the proxy class. The following table discusses the important properties of InvokeWebService
activity that you need to work with.
Table 1: InvokeWebService Properties
| Property |
Description |
ProxyClass |
Allows you to specify the type of the proxy class. You can manually enter this information or the
workflow can automatically populate this information after you select the Web service through the Add Web
Reference dialog box |
SessionId |
Specifies the session to be used. This property is very important when you need to tie the different Web
service invocations together |
MethodName |
Allows you to specify the name of the Web service method to be invoked when the activity is executed |
URL |
Allows you to specify the URL to be used for communicating with the Web service |
As mentioned in Part-1 of this series, Windows Workflow Foundation supports two types of
workflows-Sequential and State Machine. You can use the InvokeWebService activity in both the Sequential and
State Machine workflows. From an implementation standpoint, it is similar to using a Web service in any other
.NET application. Once you reference the Web service through the InvokeWebService activity, you are ready to
execute the Web service.
To start with, create a new Visual C# Sequential Workflow Console Application named
HelloWorldServiceClient. Once the project is created, you will see a Workflow named Workflow1 being
created. Now drag and drop an InvokeWebService activity to the workflow designer. As soon as the activity is
added to the workflow, the Add Web Reference window shows up in the screen. Now type in the following URL in the
address bar:
http://localhost/MyPro
jects/DotNetSlackers/HelloWorldService/HelloWorldService.asmx
Change the Web Reference name to HelloWorldServiceProxy.
After the Web service reference is added, click the InvokeWebService activity and bring up the properties window.
Click the MethodName property and choose HelloWorld from the drop-down list. Each
InvokeWebService activity lets you interact with one Web service method exposed by a Web service.
Once you choose HelloWorld, the ReturnValue property appears under
Parameters. Through the ReturnValue property, you can bind the return value from the
Web service to a property or field in the workflow. To accomplish this, click on the ellipse button right next to
the ReturnValue property and select Bind to a new member tab in the resultant dialog box. Through
this dialog box, create a new property named HelloWorldResult as shown below.
Figure 1: Create a new property
Here is how the InvokeWebService activity property dialog box looks like after setting these
properties.
Figure 2: The InvokeWebService activity property dialog box

(Click here for larger image)
Now that you have captured the output of the Web service invocation into a local property, the next step is to
display that output. For this purpose, drag a Code activity onto the workflow designer right after the
InvokeWebService activity and name it as displayResult_Activity through the Properties
dialog box. Now double click on the empty text area right next to the ExecuteCode property in the
properties window and that will take you to the code editor with the following code populated.
Now add the following lines of code to the above event handler to display the Web service invocation result.
If you execute the workflow, you will see the following output being displayed on the screen.
Figure 3: Display the Web service invocation result

(Click here for larger image)
Exposing a Workflow as a Web Service
So far, you have understood the steps to be followed for invoking a Web service from a workflow. With that
background, let us now look at the steps involved in exposing a workflow as a Web service. As mentioned
previously, workflow runtime executes workflows asynchronously because of which using a workflow from an
ASP.NET application is not that straightforward. For example, if an ASP.NET request
(for a Web service or a Web page) comes in, the workflow instance begins execution and the runtime returns
control to ASP.NET. Your Web service or Web page immediately continues preparing output and likely
finishes before the workflow instance completes. Because of this asynchronous nature of the workflow execution
process, your ASP.NET code could easily finish and return a response to the caller without ever
completing the workflow processing. To avoid this behavior, you need to ensure that the workflow runs
synchronously, which you can accomplish through specific configuration entries in the web.config file.
Creating the Workflow
To create a workflow as a Web service, you first need to create an interface and expose the methods
of that interface as Web service methods. Typically the interface and the workflow are
hosted in a separate assembly and then referenced from within the ASP.NET Web service project.
However once you create the interface and workflow, you don’t need to manually create the
ASP.NET Web service project as Visual Studio can auto-generate the ASP.NET
Web service project for you.
Workflow Activities for Exposing a Workflow as a Web Service
The following table discusses the three key activities that are going to have to work with to be able to expose
the workflow as a Web service.
| Activity |
Description |
WebServiceInput |
Each workflow based Web services require at least one WebServiceInput activity and one or
more WebServiceOutput activities. As the name suggests, the WebServiceInput activity
accepts the input parameters for future processing by the subsequent steps in the workflow |
WebServiceOutput |
This activity completes the Web service’s processing by returning the return value identified by the
MethodName in the WebServiceInput activity. For this reason, you must set the
InputActivityName property to one of the WebServiceInput activities present in your workflow. You
can have multiple output activities for a single input when the activities are placed in parallel execution
paths |
WebServiceFault |
This activity allows you to bind the exceptions generated during the Web service execution and translates
them into SoapException objects. Closely related to WebServiceOutput activity in that
it also indicates the termination of workflow processing |
Let us create the workflow by creating a new Visual C# Sequential Workflow Library project named
EmployeeWorkflow from Visual Studio 2005. Once the project is created, add a new class
named IEmployeeApproval.cs and modify its code to look as follows:
The above interface is the one that consumers will see when they want to use the service. It
defines any methods or properties that will be implemented by the Web service.
Now that you have an interface to build from, return to the workflow designer by selecting
Workflow1.cs in the EmployeeWorkflow project and then clicking the View Designer button
from the Solution Explorer toolbar. Drag an instance of WebServiceInput activity onto your workflow
designer from the toolbox. Now let us configure the properties of WebServiceInput activity as shown
below.
- Interface -
EmployeeWorkflow.IEmployeeApproval
- IsActivating –
True
- MethodName –
ProcessWorkflow
Note that the IsActivating property needs to be set to True for the first
WebServiceInput activity in a workflow. When an application using this web service makes a call to
this web service and executes ProcessWorkflow, the workflow engine will know to call this
WebServiceInput activity.
Remember the ProcessWorkflow method has an input parameter named workflowName that needs to be
added as part of the parameters. Click the ellipse right next to the workflow property under the
Parameters section in the Properties dialog box. Click the Bind To A New Member tab,
and type WorkflowName in the New Member Name field. After making sure the Create Property option is
selected, click OK.
Now drag a Code activity from the Toolbox, and drop it below webServiceInputActivity1. Enter
ProcessWorkflow as its ExecuteCode property. Modify its ProcessWorkflow method
implementation to look as follows:
Now drag a WebServiceOutput activity onto the designer and drop it below the Code activity that
was inserted in the previous step. Assign webServiceOutputActivity1’s InputActivityName to be
webServiceInputActivity1 through the selection in dropdown box. As soon as you do that, note that
(ReturnValue) is added to the list of webServiceOutputActivity1 properties.
Since you want to return a string as the output from the Web service, you need to bind a workflow property to
the (ReturnValue) property of webServiceOutputActivity1. Click on the ellipse button
right next to the (ReturnValue) property. In the resultant dialog box, navigate to the Bind To a New
Member tab and create a new property named EmployeeWorkflowResult.
Now that you have created the workflow, the next step is to expose the workflow as a service so that the
client applications can execute the workflow as a service.
Exposing the Workflow as a Service
The final step to make a workflow into a web service is to publish the workflow as a web service. To do this,
right-click the on the EmployeeWorkflow project in the Solution Explorer and choose Publish as Web
Service. As a result of this, a new solution will be created within the Solution Explorer, and all the necessary
files are also created. If you open the Web.config file, you will see the configuration entries required to run
the workflow in a synchronous manner. To view the created Web service, right click the file with the .asmx
extension and choose View In Browser. This opens the .asmx file in the browser wherein you can test the
Web service.
Invoking the Workflow Web Service
Now that you have created the Web service, the next step is to write a client application to invoke the Web
service. For this purpose, create a new Visual C# Console application named EmployeeServiceClient.
Once the project is created, add a Web reference to the Web service. To invoke the Web service, add the following
lines of code to the Main method in the Program.cs file.
If you run the application, you will see the below output.
Figure 4: Workflow Output

(Click here for larger image)
Conclusion
Although the examples presented in this installment were simple, they covered some key points. First, you learned
how to invoke a Web service from within a workflow. After that, you understood the steps involved in exposing the
workflow as a Web service. The next installment in this series will get into the various features of
WF including creating a custom activity, and invoking a workflow from another workflow and so on.
Downloads
About Thiru Thangarathinam
 |
Sorry, no bio is available
This author has published 4 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
GiveCamps Get a new Sponsor
read more
WPF 4 (VS 2010 and .NET 4.0 Series)
read more
Foxit PDF Previewer update
read more
Announcing the WebsiteSpark Program
read more
More On The CodePlex Foundation
read more
My History of Visual Studio (Part 6)
read more
Scenarios for WS-Passive and OpenID
read more
My History of Visual Studio (Part 3)
read more
My History of Visual Studio (Part 2)
read more
SOA Patterns presentation on E-VAN (recording)
read more
|
|
Please login to rate or to leave a comment.