WF Tutorial, Part 3: Create and host Workflow services


16. February 2015 Workflow Foundation 4

This tutorial is the third part of Dispatcher Timer Windows Workflow Foundation tutorial series.

<< Create a simple Workflow

This tutorial demonstrates how to create a simple Workflow Service and host it in a Web Application. This sample is using Visual Studio 2013 with .NET 4.5.1. The example simply gets a value as the service parameter, squares it, and returns it to the service caller.

In this tutorial:

Create a Workflow service activity

Host the Workflow Service

Test the Workflow Service

Create a Workflow service activity

  • Open Visual Studio and create a New Class Library.

Remove the Class1.cs which Visual Studio creates by default, from the project.

  • Add a new Workflow Activity to the project and name it SquareActivityService.xaml

Note that the following references will be added to the project. These references are necessary for Workflow projects.

  • Open the SquareActivityService.xaml and from the Toolbox drag and drop the ReceiveAndSendReply Activity under Messaging group to the designer.

The following activities will be added to the designer.

  • Select the Sequence, open the variable tab, and add the following variables:

_number will be used as input to the service and after calculation the result will be stored in _result and will be returning to the service caller.

  • Drag and drop an Assign activity under Primitives in to the designer between the Receiveand SendReplyToReceiveactivities.

    In the To type: _result

    In the C# expression box type: _number * _number

  • Click on the Receive activity and change the OperationName to “Calculate”.

  • In the Receive activity click on the box next to Content. Expand the Parametersand Add a new parameter as below.

In this step we are defining our service parameters. In this case we create a parameter number on type Int32 and assigned it to the _number variable which we have created before. The same way other parameters can be added to the workflow service.

  • In the SendReplyToService activity click on the box next Content. Expand the parameter and Add a new parameter as below:

In this case we are defining the data which our service gonna return back to the caller. Here we have added a parameter called result of type Int32 which will be store the data from the variable _result.

  • Click on the Receive activity and open the Property Window (Press F4). Modify the properties as below.

  • Take note that the Operation Name is “Calculate”. This will be the same name as the operation in WCF. The caller will see this as a method so she can call it from the service proxy.
  • Change the ServiceContractName to “ISquareService”. This will be the service contract which is the same as WCF service contract. Make sure to put an “I” before the name as in WCF service contracts are an Interface (just to make our naming standard).
  • Check the CanCreateInstance check box. This is to enable the workflow service to be able to return the result through the proxy back to the caller.
  • That’s all J

Host the Workflow Service

  • In the same solution create a New ASP.NET Empty Web Application and name it “SquareService.Host”.

  • Add a reference to SquareService project.

The solution looks like below:

  • Open the Web.Config and modifies it as below

Take note of the highlighted sections:

  • The relative address in the service activation is the service address which we can access to the service.
  • The service section is the full qualified service name, which the same name as our activity name.
  • In the service endpoint section the contract name is the same name we have specified in the Receive activity properties.

Test the Workflow Service

  • Set the web application as startup project in the solution and run it (by either press F5, or pressing Ctrl+F5).
  • The service will be opened in a browser. Enter the service relative address in the browser.

You will see the service is up and available. You can also look in to the WSDL definition by click on the links provided in the page.

  • Open Visual Studio WCF Test Client to test the service. Add the service in the WCF Test Client.

You will see that the Calculate operation of our work flow service is available.

  • Double click on Calculate operation to open it. In the Request section enter any number and press the Invoke button. You will see the result of the calculation will be appearing in the Response section.

Take note that the variable name is the same as what we have define in the list of parameters in our Receive activity.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s