views:

106

answers:

3

How do I save the XAML for a WF4 workflow in a rehosted designer, without writing it to a file? I want to store the serialised workflow in a database as an XML string.

Given a WorkflowDesigner instance called w, this saves to a file fine:

WorkflowDesigner.Flush();
w.Save("filename.xaml");

I was hoping this would serialise to a string - but it fails:

WorkflowDesigner.Flush();
var modelService = WorkflowDesigner.Context.Services.GetService<ModelService>();
var workflow = modelService.Root;
var xml = XamlServices.Save(workflow);

... while saving a single Sequence activity, it says "Type 'System.Activities.Presentation.Model.ModelItemImpl' not visible. If the type is local, please set the LocalAssembly field in XamlReaderSettings.

A: 

Hmmm, I haven't used this; the API seems a bit... odd. But from the docs, it appears that when you call Flush() on the instance of the WorkflowDesigner, it saves the current workflow to a public property called Text. You should be able to grab that text and stick it in your database just fine.

From the docs:

Before getting this value, call Flush() to convert the current workflow to text. After setting this value, call Load() to load the XAML into the workflow.

Will
+1  A: 

Yes, use Flush() and Text to get the xaml as a string. Use Save() to save to a file or stream, no Flush() needed in that case.

Maurice
The Text property seems to have some extra tags in the XML for viewstate, but it works fine.
Andrew
The ViewState elements are for the designer and also there is you save to file or stream. You can safely remove all the extra elements and attributes and you workflow will execute just fine. The VS experience, like debugging and stepping through the workflow, will degrade though.
Maurice
A: 

Using ActivityXamlServices and a StringWriter to flush the content to a string.

Isaac Yuen