Workflow as a Web Service
Using the WebServiceInput, WebServiceOutput, and WebServiceFault activities, you can expose your workflow as a web service. This has some advantages, especially within SOA environments and when deploying a Windows-based application instead of a web-based application. In this section, I present an example of a simple workflow that's exposed as a web service. First, create a VB Sequential Workflow Library called "VBAsWebServiceSequentialLibrary."
Make sure you select Sequential Workflow Library. Change the name of the Workflow1.vb file to "WorkflowAsService.vb." Open the workflow file (WorkflowAsService.vb) and view the code. Add Public ValueEntered As Integer to the workflow class declaration. This variable holds the inputted value and allows manipulation of it. Next, you must define the interface that will be exposed. An interface is what other applications wanting to use this web service will see. It defines any methods or properties that other applications can use to interact with the web service, and is a core OOP concept. To define the interface, add the following:
Public Interface IWorkflowAsWebService Function AcceptValue(ByVal InputValue As Integer) As Integer End Interface
Any application wanting to use this web service will see only the AcceptValue method, and will see that this method expects a parameter that is of type Integer. Next, view the designer and add a WebServiceInput activity to the workflow. Leave the default name, but click the ellipse next to Interface Type. This displays a window that lets you pick a .NET Type. You want to select the interface that was just defined. You can do this by clicking the Current Project folder on the left and selecting the defined interface, as in Figure 5.
Set IsActivating to True, because the first WebServiceInput activity within a workflow must have the IsActivating property set to True. From the drop-down list next to the Method Name property, choose AcceptValue. This links the call to the WebServiceInput activity to the interface. When an application using this web service makes a call to this web service and executes AcceptValue, the workflow engine will know to call this WebServiceInput activity. Finally, from the drop-down next to InputValue under the parameters, select IntInputValue. Click the Promote Bindable Properties link at the bottom of the Properties window. Click the ellipse next to the InputValue property and choose ValueEntered from the Bind Properties dialog box. This ties the value passed in as a parameter to AcceptValue to the ValueEntered variable defined within the workflow. The properties of the WebServiceInput activity look like Figure 6.
Add a WebServiceOutput activity to the workflow. Leave the name as the default. Use the drop-down next to InputActivityName to select WebServiceInputActivity1. This property ties the two activities together, so the input activity is receiving information, the output is sending the information, and it's the same information. Click the Promote Bindable Properties link at the bottom of the Properties window. Click the ellipse next to the Return Value property and choose ValueEntered from the Bind Properties dialog box. Again, this ties the value returning to the application that calls this web service to the variable declared within this class.
Next, click GenerateHandlers at the bottom of the Properties window. This generates code to be executed when the activity is executed. When the sub is created, add:
IntInputValue = IntInputValue + 10
This simply adds 10 to the value provided by the calling application. Now the workflow is complete. The workflow will accept a value, add 10 to that value, and return it to the calling application. This simple example shows how you can use a workflow as a web 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 project name within the Solution Explorer and choose Publish as Web Service. A solution is created within the Solution Explorer, and all the necessary files are also created. To view the created web service, right-click the file with the .asmx extension (VBAsWebServiceSequentialLibrary.WorkflowAsService_Web-Service.asmx) and choose View In Browser. This opens the .asmx file in the browser. Click the AcceptValue link to test the AcceptValue method. Enter 10 as the InputValue and click the Invoke button. The result of 20 appears in the XML file. This shows the workflow working as a web service.