Speeding up your CCF Workflow

CCF Workflows takes several seconds to start for the first time. This is to do with the fundamental .Net feature called JIT. When you first launch the workflow, CCF creates the workflow engine for the first time and starts the engine before working on your request. This eats up so much of processor... remember the JIT compiler compiles the IL instructions only when required?

One workaround for this problem would be warm up your workflow before the user actually uses this. This will help in faster response time from CCF as the required last minute compilations are already done for you and all you will do is to use the cached binary instructions instead of IL.

As always, below is the sample code that does exactly what is mentioned in the above sentence:

Find the line in your desktop.cs file

 customerWorkflowManager = (IWorkflowManager)this.GetHostedApp("Customer Workflow Manager"); 

add the following lines below that statement.

  //Modified for warming up the workflowThreadStart ts = new ThreadStart(ExecuteAsyncWorkflow);<br>Thread wfThread = new Thread(ts);<br>wfThread.Start(data);<br>//end of modifications


Finally, add this method.

 private void ExecuteAsyncWorkflow()<br>{<br>    System.Reflection.MethodInfo method = customerWorkflowManager.GetType().GetMethod( "DoAction", new Type[] { typeof(Microsoft.Ccf.Csr.Action), typeof(string) });<br>    Microsoft.Ccf.Csr.Action action = new Action(-100, "Human Workflow Automation", ""); 

    //TODO: Fill the below ALL CAPS words with proper values or better yet, make a web service call to get a valid workflow xml.
    //      Refer to custom workflow manager project on how to use the web service call.
     string data = @"
<Workflow>
<StepName>NAME HERE</StepName>
<HostedApplicationId>ID FROM DATABASE HERE</HostedApplicationId>
<HostedApplicationName>VALID APPLICATION NAME HERE</HostedApplicationName>
<Action>VALID ACTION NAME HERE</Action>
</Workflow>";

    try
{
method.Invoke(customerWorkflowManager, new object[] { action, data });
}
catch (Exception) { }
}

This is fairly simple code could save you up to 20 seconds. The tradeoff with this approach (of course all good things have an asterix) you are adding more load during the start up, though the time your solution loads will not change as this is a background thread, but the CPU is now split with one additional thread.

Let me know if you have a better solution to this problem.