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

The concept of WorkflowIdentity has been introduced 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.

C# (178.1 KB)
 
 
 
 
 
5 Star
(2)
1,516 times
Add to favorites
10/21/2011
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
//**************************************************************************************
//**************************************************************************************
//This C# project corresponds to the following Windows Workflow Foundation 4.5 Sample:
// 
//
//      "Side-by-Side Hosting of Two Versions of a Workflow Using WorkflowServiceHost"
//
//
//For a detailed explanation about this sample, please refer to the Microsoft Word Document
//that is included in the Solution folder of this sample.
//**************************************************************************************
//**************************************************************************************


//TODO [00]: Determine which version you would like to support
//*************************************************************
//In this sample, we start with supporting only one version of the service 
// which we call Version (1,0,0,0) in the sample. In order to so, the following line 
//should not be commented.  
#define OnlySupportV1
//Later on in the sample and in order to support both versions of the service 
//- Version (1,0,0,0) and Version (2,0,0,0), you would need to comment the 
// "#define OnlySupportV1" line above and uncomment the following line which 
//will enable support for both version of the service.
//#define SupportV1AndV2

using System;
using System.Activities;
using System.Activities.DurableInstancing;
using System.ServiceModel.Activities;
using System.ServiceModel.Description; 

namespace WFSHSxS
{

    class Program
    {
        static void Main(string[] args)
        {   
            //TODO [01]: Console configuration [Not Neccessary]
            //*************************************************************
            string myConsoleTitle = "Side-by-Side Hosting of Two Versions of a Workflow Using WorkflowServiceHost";
            Console.Title = myConsoleTitle;


            //TODO [02]: Setting up Version 1.0.0.0 of the service
            //*************************************************************
            WorkflowService myOriginalService=new WorkflowService
                                            {
                                                Name="Quote",
                                                Body=new Workflow1(),
                                                DefinitionIdentity=new WorkflowIdentity
                                                                        {
                                                                            Name="Quote_Old",
                                                                            Version=new Version(1,0,0,0)
                                                                        }
                                            };


            #if SupportV1AndV2
            //TODO [02]: Setting up Version 2.0.0.0 of the service
            //*************************************************************
            WorkflowService myNewService = new WorkflowService
                                            {
                                                Name = "Quote",
                                                Body = new Workflow2(),
                                                DefinitionIdentity = new WorkflowIdentity
                                                                        {
                                                                            Name = "Quote_New",
                                                                            Version = new Version(2, 0, 0, 0)
                                                                        }
                                            };
            #endif



            #if OnlySupportV1
            //TODO [03]: Setting up the WorkflowServiceHost to host Version 1.0.0.0 of the service
            //*************************************************************
            WorkflowServiceHost myHost = new WorkflowServiceHost(myOriginalService, new Uri("http://localhost:8080/LoanService"));
            #endif

            #if SupportV1AndV2
            //TODO [03]: Setting up the WorkflowServiceHost to host Version 1.0.0.0 and Verison 2.0.0.0 of the service side-by-side
            WorkflowServiceHost myHost = new WorkflowServiceHost(myNewService, new Uri("http://localhost:8080/LoanService"));
            myHost.SupportedVersions.Add(myOriginalService);
            #endif

            //TODO[04](**Modify before running**): Setting up the Instance Store for WorkflowServiceHost 
            //***********Important Notice*************
            //You need to modify the following line of code before running the sample.
            //Replace the value of myConnectionString with the value of the "Connection String" property 
            //of the WorkflowInstanceStore database on your own machine.  
            string myConnectionString = @"Data Source=localhost;Initial Catalog=PersistenceDB;Integrated Security=True";
            SqlWorkflowInstanceStore myInstanceStore =new SqlWorkflowInstanceStore(myConnectionString);
            myHost.DurableInstancingOptions.InstanceStore = myInstanceStore;
            myHost.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });
            
            //TODO [05]: Initializing the service by opening the host           
            myHost.Open();
            Console.WriteLine("Windows Workflow Foundation (WF4.5) \n*************************************\n");
            Console.WriteLine("This sample illustrates the\n\n\n\"Side-by-Side Hosting of Two Versions of a Workflow Using WorkflowServiceHost\"\n\n");
            Console.WriteLine("The host is ready for receiving service requests...\n\n");
            Console.WriteLine("Press any key to stop the service");
            Console.ReadKey();
            //TODO [05]: Stopping the service by closing the host
            myHost.Close();
        }

    }
}