Published: 06 Oct 2010
By: Harry L

In this article we will explore into one of the major features introduced in .Net 4.0, Workflow Services. Workflow Services let you define services where service operations are defined using workflow activities.

Contents [hide]

Introduction

When we think from Service oriented architecture, we always think of building business logic as a service. Suppose, for example, we are writing some functionality which must be accessed from a browser through ASP.NET, from a desktop application and also from Silverlight client application. Implementing this logic as a set of operations that can be invoked from any of these clients, as a service, is likely to be the best approach. Exposing logic as a service also makes it accessible to other logic, which can sometimes make application integration easier. (This is the core idea behind the concept of SOA, service-oriented architecture.)

Today Windows Communication Foundation (WCF) being the most favored technology for creating services in .Net, which allows us to implement business logic, accessible using Representational State Transfer (REST), SOAP, and other communication protocols. For some sort of services, WCF services can be sufficient, like service whose operations only expose data not the core business logic. But as far as more complex situations are concerned, where the operations in a service implement a related set of functionality, WCF alone will not be sufficient. If you think in real time scenarios where customers do online on line transactions like shopping, book online Air Tickets or carry out some other business process, you might well choose to use Work Flows to implement the service's logic. This combination where logic is implemented using Work Flow and exposed via WCF is known as a workflow service.

Work Flow Basics

Before digging into Workflow Services, we will have quick look at basic understanding of workflows. All workflows are implemented by composing activities, and the root layout that defines a workflow is itself is an activity. There are basically tow workflow activities available in Workflow Services, namely: Sequence and Flowchart.

Sequence: In this type activities are executed in sequence that is one after another. The sequence can contain If activities, While activities, and other kinds of control flow. It's not possible to go backwards in sequence activities, however execution must always move forward.

Flowchart: In this type activities are executed one after another, like a Sequence, but also allow control to return to an earlier step. This is more flexible approach, which got introduced in the .NET Framework 4, is closer both to how real processes work and to the way most of us think. While both Sequence and Flowchart can act as the outermost activities in a workflow, they can also be used within a workflow. This lets these composite activities be nested in random ways.

Building a Workflow Service

Now we will see how to create the Workflow Services in Visual Studio 2010. The process of creating a workflow service is somewhat different from the process of creating conventional WCF service. In WF Services we are going replace lines of code in a service operation's implementation with a composition of activities, that is why it's also termed as Declarative Services.

Following are the major differences in WF services compared to WCF services:

  • In WF Services, Service contracts are implicitly defined
  • In WF Services, implementation of Service operation is achieved with workflow activities
  • In WF Services, Service definitions are completely declarative XAML files (can be seen by right click on the xamlx file and click on view code)

To demonstrate how to create Workflow Services, we will create simple WF service and console application to test our service. I am going to create UserManagerService which will have one operation CheckUser to check the user existence. Here I have hardcoded the user check to harry, but in real time we can add business logic here. I have created User class, which will hold user ID, and user name properties.

Following are the steps to create workflow service in Visual Studio 2010.

Click on Create New Project in Visual Studio 2010. Select Workflow template under Visual C#. Select WCF Workflow Service Application as shown in following figure. Give name to project as UserManagerService. Click on OK.

By default Visual Studio will create Service1.xamlx file. Delete this file and add new workflow service file by Add New Item -> Workflow -> Workflow Service File. Change the name of the file to UserManager.xamlx.

Before configuring activities we will create variables that we are going to use in our sample service. To create variables in service click on the variables at the bottom of the service window. It will pop up variables window where we can define variables. We will create two variables user (will act as input) and result (will act as an output). To set the data type of user, select variable type cell and click on "Browse for type", there you can select UserManagerService.User.

Here we can see that there are two activities present in Sequence activity, ReceiveRequest and SendResponse. We need to configure these activities as per our requirements.

ReceiveRequest is used to configure the Contract Details of the WF Service. By Default we can see GetData operation as we get in WCF service contract, under this activity. You can modify name of operation as per requirement. We will change this to CheckUser which will check the existence of the user "harry" (in real time it can be used to check the user existence in DB). Also change the following properties for the operation.

  • OperationNane : Change this to CheckUser
  • ServiceContractName : {http://tempuri.org/}IUserManager
  • CanCreateInstance : True
  • Content: Change the default content properties as shown in figure. This will become the input parameter of the operation when we consume this service, which we will see in next section.

Now we will configure SendResponse activity, which is responsible for setting the return type of operation defined in ReceiveRequest activity. As CheckUser operation validates the incoming user name and return true/false based on validation, we will configure the return type as Boolean. To do so click on the content property of SendResponse activity and set the parameter as shown in figure.

We can change the activity name by changing the display name property. Also you can see that in the property window of SendResponse activity, Request property is set to RecieveRequest and it's in read-only mode.

Now we will add one IF condition to check whether user exists or not. For our example I have added simple IF condition block and checking whether user name supplied as input parameter is "harry" or not. If yes then return true else return false. In real world scenarios we can add business logic and here.

That's all; our sample User Manager Service is ready.

To test this service we will add one sample test console application and add this service as service reference as we do with WCF service. Following is the code snippet of calling this service.

Conclusion

In this article we have seen how easy it is to create the basic Workflow services. This gives us best of Workflow as well as of WCF services.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Harry L

Sorry, no bio is available

This author has published 2 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Android for .NET Developers - Location and Maps
In Windows Phone and iOS getting the current position of the device in terms of latitude and longitu...
Android for .NET Developers - Using Web Views
In this article, I'll show a native app that contains a web-based view. The great news is that HTML ...
Android for .NET Developers - Building a Twitter Client
In this article, I'll discuss the features and capabilities required by an Android application to ta...
Developing a Hello World Java Application and Deploying it in Windows Azure - Part II
In this article we will see the steps involved in deploying the WAR created in the first part of thi...
Ref and Out (The Inside Story)
Knowing the power of ref and out, a developer will certainly make full use of this feature of parame...

You might also be interested in the following related blog posts


Exporting SWF & FLV format reports in SSRS 2005 and 2008 read more
Project Turing: Beginning RIA Svcs read more
HanselMinutes Interview on RIA Services read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 25: ViewModel read more
Project Turing: Beginning RIA Services read more
TechEd South Africa Slides and Code - .NET RIA Services and ViewModel read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 18: Custom Linq Provider read more
Remove the business context from your services read more
New MSDN Article on RIA Services Published read more
Release Roadmap for .NET RIA Services read more
Top
 
 
 

Discussion


Subject Author Date
placeholder missing step Logan Manning 3/1/2013 4:13 PM

Please login to rate or to leave a comment.