Recently, I’ve been looking a way to persist the status of an idling Workflow on WF4. There is a way to use SQL Azure to achieve this, after modifying the scripts because they contain unsupported T-SQL commands, but it’s totally an overkill to use it just to persist WF information, if you’re not using the RDBMS for another reason.

I decided to modify the FilePersistence.cs of the Custom Persistence Service sample in WF 4 Samples Library and make it work with Windows Azure Blob storage. I’ve created two new methods to Serialize and Deserialize information to/from Blob storage.

Here is some code:

   1: private void SerialiazeToAzureStorage(byte[] workflowBytes, Guid id)
   2:      {
   3:          var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
   4:          var container = account.CreateCloudBlobClient().GetContainerReference("workflow_persistence");
   5:  
   6:          var blob = container.GetBlobReference(id.ToString());
   7:          
   8:          blob.Properties.ContentType = "application/octet-stream";
   9:          using (var stream = new MemoryStream())
  10:          {
  11:              stream.Read(workflowBytes, 0, workflowBytes.Length);
  12:              blob.UploadFromStream(stream);
  13:          }
  14:      }
  15:  
  16:      private byte[] DeserialiazeFromAzureStorage(Guid id)
  17:      {
  18:          var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
  19:          var container = account.CreateCloudBlobClient().GetContainerReference("workflow_persistence");
  20:  
  21:          var blob = container.GetBlobReference(id.ToString());
  22:  
  23:          return blob.DownloadByteArray();
  24:      }

Just make sure you’ve created “workflow_persistence” blob container before using these methods.

PK.