Workflow Persistence Service using SQL Server Compact

As I mentioned previously I was working in a Workflow Persistence Service using SQL Server Compact as the back end store. Now this might not be the best persistence store to use in every application but it has a number of advantages.

One of the main advantages is ClickOnce deployment. In the case of the standard SqlWorkflowPersistenceService you need to have SQL Server installed and running and create the database, with a name of your choosing, using two scripts. Not a big problem but something that needs to be done first and is outside of the realm of what you would like to do with ClickOnce. Not so with SQL Server Compact. No with SqlCe you can just deploy a number of DLL’s with your application and you are ready to go, so much easier and very ClickOnce compatible [:)] Add easy installation is just what you want when you are creating sample applications.

I actually did all the data access using LINQ to SQL which fully supports SqlCe as a client. It might appear not to as there is no designer support like with SQL Server itself but runtime it works just fine [:)]. using LINQ to SQL actually made the data access very easy and has an additional benefit, if you want to use the full SQL Server all you need to do is provide a different database connections string. One additional benefit of using LINQ to SQL was that is can check if the database exists and, if not, create it with a single function call. No need to mess with those tedious Create Table statements in SQL scripts!

Imports TheProblemSolver.Workflow.Hosting.Persistence

Module Module1
    Class Program
        Shared Sub Main()
            Using workflowRuntime As New WorkflowRuntime()

                Dim connectionStr As String = "Data Source=WorkflowPersistenceDatabase.sdf"
                Dim unloadOnIdle As Boolean = True
                Dim loadingInterval As TimeSpan = TimeSpan.FromSeconds(15)
                Dim persistence As New SqlCeWorkflowPersistenceService(connectionStr, _
                                                                       unloadOnIdle, _
                                                                       loadingInterval)
                workflowRuntime.AddService(persistence)

                Dim workflowInstance As WorkflowInstance
                workflowInstance = workflowRuntime.CreateWorkflow(GetType(Workflow1))
                workflowInstance.Start()

                Console.WriteLine("Press enter to stop...")
                Console.ReadLine()
            End Using
        End Sub
    End Class
End Module


Usage is real simple and pretty much the same as the standard SqlWorkflowPersistenceService type. The only extra is to set a reference to the TheProblemSolver.Workflow.Hosting.Persistence.dll that includes the SqlCeWorkflowPersistenceService type.

using System;
using System.Workflow.Runtime;
using TheProblemSolver.Workflow.Hosting.Persistence;

namespace TestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                string connectionStr = @"Data Source=WorkflowPersistenceDatabase.sdf";
                bool unloadOnIdle = true;
                TimeSpan loadingInterval = TimeSpan.FromSeconds(15);
                SqlCeWorkflowPersistenceService persistence = 
                    new SqlCeWorkflowPersistenceService(connectionStr, unloadOnIdle, loadingInterval);
                workflowRuntime.AddService(persistence);

                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1));
                instance.Start();

                Console.WriteLine("Press enter to stop...");
                Console.ReadLine();
            }
        }
    }
}

If you want to download the SqlCeWorkflowPersistenceService you can find it here. Any feedback, hopefully good, will be appreciated!


 


Enjoy!

www.TheProblemSolver.nl
http://wiki.WindowsWorkflowFoundation.eu

One thought on “Workflow Persistence Service using SQL Server Compact

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>