none
Access workflow variables in a custom instance store

    Pergunta

  • I’m struggling with a requirement to provide continuous access to variables of a workflow even if the workflow has been unloaded from memory and persisted to a DB. Meaning at the point in time when the workflow lives in the DB only, I still would like to be able to read the workflow variables with another client application accessing the DB. (Writing variables would be very cool but creates other issues, which I would like omit here to keep the discussion simple.)

    In WF4 the SqlWorkflowInstanceStore provides a way to persist workflows in a SQL database. Unfortunately the data is stored as a binary BLOB which is opaque to other clients.

    One idea to solve this problem is to create a custom instance store class that persists the workflow data including variables in a readable format in the DB. To accomplish this you need to create a CustomInstanceStore class which derives from InstanceStore and overrides BeginTryCommand and   processes the SaveWorkflowCommand.

    In the code below I’ve tried to access the workflow data,  that is stored (AFAIK) in the InstanceData property under the key “Workflow”.  So far so good. Unfortunately the value is of type System.Activities.Runtime.ActivityExecutor which is a private class I cannot access.

    protected override IAsyncResult BeginTryCommand(InstancePersistenceContext context, InstancePersistenceCommand command, TimeSpan timeout, AsyncCallback callback, object state)
    {
    	switch (command.GetType().Name)
    	{
    		case "SaveWorkflowCommand":
    			try
    			{
    				SaveWorkflowCommand createCmd = command as SaveWorkflowCommand;
    				XName key = XName.Get("Workflow", "urn:schemas-microsoft-com:System.Activities/4.0/properties");
    				InstanceValue value = createCmd.InstanceData[key];
    				Console.WriteLine("Value type:" + value.Value.GetType().ToString());
    				// Value type:System.Activities.Runtime.ActivityExecutor
    				// ActivityExecutor is private! 
    				// Is there a way to access the variables stored in ActivityExecutor?
    			}
    			catch { }
    			break;
    				
    			// Implemantation of other commands ...
    	}
    }

    I’m able to serialize and persist the ActivityExecutor as XML. However I cannot access the variables via the ActivityExecutor object in the code snipped below  (and I think using reflexion or parsing the XML is not the right way).

    Is there a way to somehow access the variables of a workflow in ActivityExecutor?

    Is there some other way to access the variables in the context of a custom instance store?

    Am I making something wrong?

    Many Thanks

    Roman


    Roman

    sexta-feira, 20 de abril de 2012 16:45

Respostas

  • Hi Roman,

    I believe you don't have to create a custom instance store to accomplish this. Being your workflow a service (xamlx) or not (xaml) you can accomplish this using a PersistenceIOParticipant. 

    Overriding the BeginOnSave, you have access to the writeOnlyValues (if you flagged your variables as Mapped in variable properties) which give you clear access to your variables immediatly before saving them into the instance store.

    This way you can get your variable whatever type it is and send it to wherever you want. 

    You can also check the Microsoft samples, human resources sample writes custom information as it evolves during its lifecycle.

    Hope this helps.

    • Marcado como Resposta Roman Boll segunda-feira, 23 de abril de 2012 08:26
    sexta-feira, 20 de abril de 2012 16:57

Todas as Respostas

  • Hi Roman,

    I believe you don't have to create a custom instance store to accomplish this. Being your workflow a service (xamlx) or not (xaml) you can accomplish this using a PersistenceIOParticipant. 

    Overriding the BeginOnSave, you have access to the writeOnlyValues (if you flagged your variables as Mapped in variable properties) which give you clear access to your variables immediatly before saving them into the instance store.

    This way you can get your variable whatever type it is and send it to wherever you want. 

    You can also check the Microsoft samples, human resources sample writes custom information as it evolves during its lifecycle.

    Hope this helps.

    • Marcado como Resposta Roman Boll segunda-feira, 23 de abril de 2012 08:26
    sexta-feira, 20 de abril de 2012 16:57
  • I've just tested your suggestion and it works perfectly.

    Thank you

    Roman


    Roman

    segunda-feira, 23 de abril de 2012 08:26