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.


WF Tutorial, Part 2: Create a simple Workflow


15. December 2013 Workflow Foundation 13

This tutorial is the second part of Dispatcher Timer Windows Workflow Foundation tutorial series.
<< An Introduction to Windows Workflow Foundation
Create and host Workflow services >>

Download the sample project

In this tutorial I’m going to show you how you can create a very simple Workflow application to ask the user’s name and greets her using C#. Visual Studio 2013 and .NET 4.5 is used in this tutorial.

Task 1: Create a new Workflow Console Application Project.

  • Open Visual Studio
  • Choose File, New, Project and Select Workflow Console Application under Installed, Template, Visual C#, WorkFlow project type.

  • Choose any project name you want and press <OK>
  • The Visual Studio will be creating a Workflow1.xaml file for you. Simply remove the file from the solution explorer.

Task 2: Create a simple Workflow to display a message

As it has been mentioned in the introduction to this WF tutorial series everything in Workflow is an activity. Activities are functional units of a Workflow. They can perform a simple task like displaying massage to the user or more complicated tasks made from combination of different other activities. We are going to use the WriteLine Activity to simply display a message.

  • Right click on the Project; Add, New Item, and select Activity from Installed, Visual C# Items, Workflow Item type.
  • Name the activity HelloLiza.xaml and Press <OK>

  • The workflow designer will be opened.
  • From the Toolbox (Ctrl + Alt + x) drag and drop a WriteLine Activity under Primitives category into the Workflow designer.

    WriteLine activity is used to display a string in a console the same way as Console.WriteLine() method.

    The Text attribute of the WriteLine Activity gets a string or you can enter a C# statement.

  • Select the Text box and enter “Hello Liza”.

Let’s run our application and see how it works. Before build the application we need to modify the Program.cs to run our new Workflow.

  • Double click on Program.cs
  • Modify the code as below

Take note that we have changed the Workflow1 initializer class to HelloLiza. For every new Workflow, we are actually creating a class. On the 5th line we are creating an Activity object based on our Workflow. WorkflowInwoker is a simple class which its Invoke
method is used to invoke a Workflow. Why I said simple class because System.Acitivities provides different methods to invoke a workflow. WorkflowInvoker is useful for synchronous execution of workflows. For long-running Workflows or Workflows which use PersistenceStores you need to use WorkflowApplication class.

To execute the Workflow:

  • Build your solution (Ctrl + Shift + B) and run your application (Ctrl + F5)

Task 3: Extend the WorkFlow to accept an input from user

We are going to extend our example and ask the user for her name and to greets her using her name and showing her the current time. To do this we need add Variables to our workflow.

  • Open the HelloLiza.xaml
  • From the Toolbox drag and drop an Assign activity above the WriteLine activity which we have added previously.

    Notice that when you drop that activity WF designer will automatically be creating a SequenceActivity for you.

You can see a small blue exclamation mark on the top-right side of the Assign and Sequence activities. This is the WF’s way to tells you that there is an error in the workflow.

  • Opens the Variables tab at the bottom-left side the Workflow Designer

Initially there is no variable in the list and you cannot even add any. You need to select the activity (the scope) which you want to add the variable to.

  • Select the Sequence activity in the designer.
  • In the Variables tab click on Create variable and type “name” for the Variable Name.
  • In the Variable Type column you can select any type. The default is String which is what we want.

Modify the Assign Activity to accept the user’s input. The Assign part of the activity accepts a variable which will be assigned the value provided on the on the textbox on the right side of the equal sign. Any C# expression can be entered to be assigned to the variable.

  • Type the variable’s name which we’ve just created (i.e. name) in the To textbox

    Notice that by typing the variable name Visual Studio provides you with intellisense same as you are writing a C# code

  • Type the following code in the Expression textbox

Take note that you can modify the attributes of an activity from the Properties window too. Especially when you are writing a pretty long C# code in the Expression text box which is so small in the WF designer. To do so just click on the activity you want to change its attribute and in the properties window click on the Browse button […] of the Value attribute. It will open a textbox which is pretty bigger than the designer so you can enter your C# expression there.

To show the entered name by user let’s modify the WriteLine activity.

  • Enter the following line in the Expression textbox (or alternatively in the Properties window)

You can see we can concatenate a string in the Expression textbox. In the line we are showing a greeting message along with the today’s date.

The last task is adding another WriteLine activity above the Assign activity asking the user to enter her name:

  • Drag and drop a WriteLine activity above the Assign activity
  • Enter the following line in the Expression textbox:

The final workflow should be the same as bellow:

  • Build the solution (Ctrl + Shift + B) and run the application (Ctrl + F5)
  • Enter your name and press enter

That’s all for this tutorial.

<< An Introduction to Windows Workflow Foundation