When Windows Workflow Foundation was originally conceived, WCF was the “hot” web service technology for .NET developers. However, since then, the industry and Microsoft have moved toward RESTful services like Web API.

While Workflow does not have native support for Web API in the same way it does for WCF (although the Workflow dev team is interested in adding support) it doesn’t mean you can’t use workflows in your Web API service!

Let’s take a look at how we can add a workflow to our Web API service.

Example:

In this example we will use a simple workflow to evaluate a loan application and determine if we will approve it or not. We will be checking the following:

  1. Does the applicant have collateral?
  2. Does the applicant have a credit rating equal to or greater than 650?
  3. Does the applicant have a down payment of greater than 20%?

First, let’s create a “Loan” model and add a few properties.

Next, let’s create a simple code activity to verify that the application has been completed.

CreditAppAddNewCodeActivity

We’re going to add an “InArgument” to take in our “Loan” and an out argument to pass back whether it was “Valid” or not. The we’ll add some code to see if the properties have been filled out. After that we we will build the solution.

Next let’s create a new “xaml” workflow.

WebApiExampleNewWorkflow

First, we will add arguments to take in our loan model and pass back our result.

CreditAppWorkflowArguments

We will also add a variable for holding the “applicationComplete” variable.

CreditAppWorkflowVariable

We’re going to add a a few “FlowDecision” activities and wire them up to check our various conditions:

  1. Is the loan application complete?
  2. Is credit rating greater than or equal 650?
  3. Does the applicant have collateral?
  4. Is the down payment at least 20% of the loan amount?

CreditAppWorkflow

Right click on each activity and we will set the “Condition”

CreditAppExpressionEditor

In the end your workflow should look like this.

CreditAppWorkflowFull

Now that we have our Workflows created we can wire them up in our controller. It’s very simple.

Now, if we call our service with parameters that ensure success we get back true.

CreditAppTrue

And if we change our credit rating to ensure a denial we get back false.

CreditAppFalse

As you can see we can very easily add Workflow capabilities to our Web API service. We can also reuse these workflows completely unaltered in any .NET application, whether it be WCF, WPF or MVC. Best of all, the logic behind this app is visual and easy to understand.

I hope this shows that workflow can be used in any of your apps and not just big enterprise WCF apps.

This sample project is available on GitHub.

Blake Helms

Blake Helms is a .NET Software Development Manager for EBSCO Industries, a global company with businesses in a range of industries including Information Services, Publishing and Digital Media, Outdoor Products, Real Estate, Manufacturing and Distribution, and Business Services, headquartered in Birmingham, Alabama. There he is responsible for several core business applications and has been a driver for software craftsmanship and creating a culture that promotes mentorship and continuous improvement. He is also the cofounder of the Birmingham .NET Meetup with a mission to promote good development practices and .NET technologies in the Birmingham, Alabama area. Blake is incredibly passionate about technology in all areas from writing code for work, to audio/video production for his church to automating his home.