With the .NET framework 4.0 approaching I’ve started trying some stuff out. One of the things is the new runtime itself in combination with persistence. My first impressions were good, the new WorkflowInstance way of working is much easier than the previous one with the workflowruntime class that you needed to instantiate. The whole persistence logic also works a little more intuitive.
There is however one catch. Workflow 3.5 is not completely phased out and that can cause some troubles when working with persistence. My first attempt at doing persistence with workflow was using the following snippet:
private WorkflowInstance CreateWorkflowInstance(Guid correlationId, int issueId)
{
SqlPersistenceProviderFactory factory = new SqlPersistenceProviderFactory(
ConfigurationManager.ConnectionStrings[“WorkflowPersistence”].ConnectionString,
true, true, TimeSpan.FromSeconds(0));
factory.Open();
PersistenceProvider provider = factory.CreateProvider(correlationId);
Dictionary<string, object> inputs = new Dictionary<string, object>();
inputs.Add(“issueId”, issueId);
WorkflowInstance instance = new WorkflowInstance(new TrackIssue(), inputs);
instance.Extensions.Add(provider);
return instance;
}
This doesn’t work, because it gives an error telling you that applications shouldn’t use a persistence provider that supports keys in their application. This is meant for declarative workflow services and not for the scenario that I was working on.
I already had setup the right database schema objects and stored procedures using PersistenceProviderSchema40.sql and PersistenceProviderLogic40.sql from the .NET 4.0 framework directory. So I disabled the use of keys by setting the second Boolean in the factory constructor to false.
SqlPersistenceProviderFactory factory = new SqlPersistenceProviderFactory(
ConfigurationManager.ConnectionStrings[“WorkflowPersistence”].ConnectionString,
true, false, TimeSpan.FromSeconds(0));
This gives a new error, saying that the workflow cannot be started because the InserInstance procedure isn’t found in the database. It’s weird, because I thought I used the right scripts.
What the workflow engine is doing here is that it creates a .NET 3.5 persistence provider instead of a .NET 4.0 version of the persistence provider. The .NET 3.5 version requires PersistenceProviderSchema.sql and PersistenceProviderLogic.sql instead of the 40 versions of the same scripts. For the rest of the instructions on how to setup persistence I can recommend the MSDN documentation.
I checked the documentation and it doesn’t tell you this, so by this writing this article that problem is solved 😛