Sample Title 

Side-by-Side Hosting of Two Versions
of a Workflow Using WorkflowServiceHost
 

Overview 

Microsoft has introduced the concept of WorkflowIdentity in Windows Workflow Foundation 4.5. This identity is stamped on each WorkflowService instance that is created by a host and allows the same WorkflowServiceHost to support multiple versions of a given definition of a Workflow. This Sample illustrates the aforementioned capability.
 

Keywords 

WorkflowSerivceHost, Side-by-Side, Versioning, WorkflowIdentity
 
  
Table of Contents
1.      Introduction
2.      Scenario
3.      Implementing Version 1 of The GetAQuote Service
4.      Deploying Version 1 of  The GetAQuote Service via WorkflowServiceHost
5.      Implementing Version 2 of The GetAQuote Service
6.      Deploying Version 1 and Version 2 Side-by-Side Using WorkflowServiceHost
 

Objectives 

This sample aims to illustrate the following new features which have been introduced in .NET Framework 4.5 for the first time:
 

Pre-requisites

Familiarity with:


Software Requirements


Build Number Used For Sample Code


Description


1. Introduction 
Microsoft has introduced the concept of WorkflowIdentity in Windows Workflow Foundation 4.5. This identity is stamped on each WorkflowService instance that is created by a host and allows the same WorkflowServiceHost to support multiple versions of a given definition of a Workflow. This Sample illustrates the aforementioned capability by implementing the following simple scenario. 
 
2. Scenario 
Contoso Loans offers 1-year loans to its customers. Through using the GetAQuote service of Contoso Loans, customers can enter their Name and then get the current interest Rate. Later on, they can come back, enter their Name and the Amount of the loan that they need; and the service will tell them the amount that they will need to pay back in a year. Since interest rates can change overnight, Contoso Loans should be able to update the GetAQuote service with new interest rates. Nevertheless, Contoso wants the customers that have received a quote based on the previous interest rate but have not determined the amount of the loan, to be able to use the previous version of the service which was based on the previous rate. As a result, Contoso Loans requires two versions of the GetAQuote Service to run side-by-side. One version, with the previous interest rate for customers who are in the middle of using the service and have received the interest rate but have not determined the amount; and another updated version for customers that will be using the service from the current time moving forward.

Please note that this is a contrived example for the purpose of showing how WorkflowServiceHost can support side-by-side versions of Workflow services. In real world scenarios, the updated version can include significant changes to
the business process.
 
This sample includes the following steps:
Please note that you would need to run Visual Studio in Administrator mode to run this sample. 
   
3. Implementing Version 1 of  The GetAQuote Service 
As we mentioned in the scenario:
“Contoso Loans offers 1-year loans to its customers. Through using the GetAQuote service of Contoso Loans, customers can enter their Name and then get the current interest Rate. Later on, they can come back, enter their Name and the  mount of the loan that they need; and the service will tell them the amount that they will need to pay back in a year.”
In order to implement this scenario we will need two ReceiveAndSendReply activities that correlate on the Name of the user.
In order to see the details of the Workflow configuration:
You should be able to see what has been shown in following picture.
In this Workflow, the second Receive activity correlates with the first one on the Name of the user. In order to verify that:
 
We use the WorkflowServiceHost for hosting Workflow1.xaml.  To see how this has been done:
C#
Edit|Remove
WorkflowService myOriginalService=new WorkflowService 
{ 
Name="Quote", 
Body=new Workflow1(), 
DefinitionIdentity=new WorkflowIdentity 
{ 
Name="Quote_Old", 
Version=new Version(1,0,0,0} 
}; 
 
WorkflowServiceHost myHost = new WorkflowServiceHost(myOriginalService, new Uri("http://localhost:8080/LoanService")); 
This sample requires that you set up a persistence store for your Workflow. In summary, you would need to create a database to persist workflow instances. To clean up the persistence database to have a fresh database, run the scripts in %WINDIR%\Microsoft.NET\Framework\v4.xxx\SQL\EN in the following order.
  1. SqlWorkflowInstanceStoreSchema.sql
  2. SqlWorkflowInstanceStoreLogic.sql
If you need further information about setting up a persistence store, you can refer to MSDN documentation (e.g. How to:Enable SQL Persistence for Workflows and Workflow Services ).  
Please note that before running the code you, in the following line of code, would need to replace the value of the  myConnectionString variable with the Connection String of the database that you are using for persistence.
C#
Edit|Remove
string myConnectionString = @"Data Source=localhost;Initial Catalog=PersistenceDB;Integrated Security=True";
You can find the Connection String of the database that you are using for persistence by following these steps:

      
4. Deploying Version 1 of  The GetAQuote Service via WorkflowServiceHost 
Now you can deploy the GetAQuote service by running the code.
You should be able to see the following Console Window.
The GetAQuote service is up and running. Now customers can connect to the service and request a  Rate.
In this sample, we will use the WCF Test Client for invoking the service.
In the WCF Test Client window:
At this point, you should be able to see a WCF Test Client window similar to what has been shown in the following window.


While this customer is considering their option; the interest rate may change overnight. While Constoso wants this particular customer to be able to borrow money based on the 1% interest rate that they have receive, new customers should be treated based on the new interest rate which is 10%. Therefore, Contoso will need a new version of the service (Version 2) running side-by-side the old version (Version 1).
    
5. Implementing Version 2 of The GetAQuote Service  
 
In order to enable support for both Version 1 and Version 2 of the service, first you will need to stop Version 1 of the service that is currently running.
The in the Program.cs file:
 In order to support the second version, we define a new WorkflowService with as following:
C#
Edit|Remove
            WorkflowService myNewService = new WorkflowService 
                                            { 
                                                Name = "Quote", 
                                                Body = new Workflow2(), 
                                                DefinitionIdentity = new WorkflowIdentity 
                                                                        { 
                                                                            Name = "Quote_New", 
                                                                            Version = new Version(2000) 
                                                                        } 
                                            };
  
 
C#
Edit|Remove
 WorkflowServiceHost myHost = new WorkflowServiceHost(myNewService, new Uri("http://localhost:8080/LoanService"));
 This will ensure hosting the latest version of the service.
 
C#
Edit|Remove
myHost.SupportedVersions.Add(myOriginalService);
 
6. Deploying Version 1 and Version 2 Side-by-Side Using WorkflowServiceHost
Now, if we rerun the program, we will have support for both versions of the service. New users (e.g. customer200) will be able to get the latest interest rate (10%), while existing users (e.g. customer100) will be able to  borrow money based on the original interest rate (1%).
 
So, if we use the WCF Test Client to invoke the service for a new user (e.g. customer200), we will get the 10% interest  rate as shown in the following figure.
 
 
 
Later on, users can come back and double click on the CalculatePaybackAmount, enter their name and the amount that they would like to borrow, and the service will tell them about the amount that they will need to pay back  at the end of the year.
For new users (e.g. Customer200) the service will use the latest interest rate (10%) as shown in the following figure.
 
  
But for existing users (e.g. Customer100), the service will use the original interest rate (1%) as shown in the following figure.
 
 

Summary

This sample illustrated how two different versions of a WorkflowService could be hosted side-by-side using WorkflowServiceHost.