How to create a workflow programmatically?
Alek
Alek
The Order Processing State Machine example on the WindowsWorkflow.net site shows how to dynamiclly update a workflow instance to add a state activity. The sample is available here. When running the OrderApplication, just right-click on an workflow instance in the list and select the item from the context menu to dynamically add activities.
James Conard
Architect Evangelist - Windows Workflow Foundation
http://www.WindowsWorkflow.net
http://blogs.msdn.com/jamescon
James,
You say that the sample "shows how to dynamiclly update a workflow instance", but this is not what I need; I need to create/modify a workflow definition, not the instance of a running workflow.
Alek
The tutorials in the SDK are written so that you don't actually use the designer at all. The sequential workflow, state machine workflow and custom activity tutorials are all code based tutorials.
As far as serializing the workflow once it's running, take a look at my blog entry here: http://blogs.msdn.com/markhsch/archive/2005/09/28/475033.aspx
Let me know if you need more help in this area.
Mark Schmidt
Programmer Writer
Windows Workflow Foundation
Microsoft
Thanks for the feedback. I checked the Simple Sequential Workflow sample, and if other samples use the same approach, this is not quite what I'm looking for. The sample contains the already programmed custom workflow class, and I am looking for a way to "create" a custom workflow class on the fly. Maybe I am not expressing myself correctly, but for some reason all the responses address the questions I did not ask. Just out of curiosity (so that I understand what I am saying wrong), why do you point to the explanation ot the serialization of the running workflow? I thought I mentioned several times that I do not care about changing workflow instances, I need to change the workflow definitions. Am I not using the right terminology? Should I phrase the question differently?
Regards,
Alek
Clearly the designer does this. Below is the code for that would define a very simple workflow (no activities added etc).
<using statments omitted>
namespace WorkflowConsoleApplication1
{
public sealed partial class Workflow1: SequentialWorkflow
{
public Workflow1()
{
this.ID = "Workflow1";
}
}
}
I'm confused as to what you mean by "on the fly" though? I think you want your custom application to generate the appropriate code which would create the workflow as it was being constructed. Is that correct?
I've been thinking about the same thing, and my guess is that the way to do it would be through creating or altering XAML files (pure code can also be generated and compiled programatically, of course). But, this is only guessing, for the moment. Anyone from MS care to butt in here?
Being able to programatically create or alter workflow definitions enables the creation of really dynamic systems, and is in my opinion maybe the most exiting part of the whole picture...
Arne
Alek Davis:It sounds like you want to build your own workflow authoring tool that doesn't use the Visual Workflow Designer control. Here's a code example that shows how to programmatically define a new Sequential Workflow with a single Delay activity and then saves it to a .xoml file. Then you can compile the .xoml file using the WorkflowCompiler class. Dharma Shukla posted an example of using the WorkflowCompiler class on his blog here.
// Create a new Sequentail Workflow
SequentialWorkflow wf = new SequentialWorkflow();
wf.ID = "SequentialWorkflow1"; // Create a new Delay activity
Delay delay1 = new Delay();
delay1.ID = "delay1";
delay1.TimeoutDuration = new TimeSpan(0, 0, 20); // Add the Delay activity to the workflow
wf.Activities.Add(delay1); // Serialize the workflow to an XML file using the WorkflowMarkupSerializer
System.IO.StreamWriter writer = new System.IO.StreamWriter(@"c:\temp\sequentialworkflow1.xml"); WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
serializer.Serialize(wf, writer);
writer.Close();
I hope that helps.
James Conard
Architect Evangelist - Windows Workflow Foundation
http://www.WindowsWorkflow.net
http://blogs.msdn.com/jamescon
Mark, Yves, thanks, the BLOG entry offers some hints.
Arne, thank you! I knew I was not crazy. :-)
James, yes, this is exactly what I was trying to say. Basically, we -- and by "we" I mean several teams working on projects with similar functionality -- are building our own workflow authoring tools, which do not use the designer. The sample you provided is helpful (although, my colleague who tried to use it on a test project encountered an error when attempting to compile a XOML-based workflow: "Attribute 'Class' not found on the root activity," but I'm not sure if this is the framework problem; will keep looking), but it is not enough. We need to know how to define activity document hierarchy (not sure if this is the right term): add/remove/reorder activities. For example, if I have two sequential activities, how do I specify which one goes first, and which one is second? If there is a conditional activity with two branches (the total of three activities), how do I specify which branch is called from which condition (again, excuse me if I do not use the right terminology)? Say, I have a workflow with three sequential activities, and I want to remove the one in the middle, swap the order of execution of the remaining two, and append a new one at the end. Which API's should I use for this? And again, I'm talking about changing the workflow definition, not the instance. Unfortunately, I cannot seem to find any references/samples explaining how to approach this; all samples that I saw deal with dynamic updates to workflow instances, not the workflow definitions. I'm pretty sure that besides our teams, there will be others who would need to do what we're doing, so some samples and general suggestions -- like which classes/methods to use when authoring workflows programmatically, etc -- would be really helpful. Thanks.
There indeed is a way to achieve what you are trying to do. You can create a workflow programmatically by using the workflow object model (which is the code that the VS designer generates in the .Designer.cs file in the background).
You can then save the workflow to a file as markup, and even compile the workflow programmatically. Our hands on labs(http://msdn.microsoft.com/workflow), specifically Lab 12 has an example of how to serialize a workflow as markup to a file. Check out the loader.cs file found in the completed lab. Here's a snippet showing the use of the markup serializer we provide:
StreamWriter writer = new StreamWriter(workflowMarkupFile); Hope this helps!
try
{
WorkflowMarkupSerializer workflowMarkupSerializer = new WorkflowMarkupSerializer();
workflowMarkupSerializer.Serialize(workflow, writer);
}
finally
{
writer.Close();
}
Angel Azcarraga
I think I can clarify several things for you. So anytime you create an activity or a workflow through code, as in one of the examples above:
// Create a new Sequentail Workflow
SequentialWorkflow wf = new SequentialWorkflow();
wf.ID = "SequentialWorkflow1";
// Create a new Delay activity
Delay delay1 = new Delay();
delay1.ID = "delay1";
delay1.TimeoutDuration = new TimeSpan(0, 0, 20);
// Add the Delay activity to the workflow
wf.Activities.Add(delay1);
you're using the object model. If you're wondering how you specify order (left to right, up and down, etc.), it's basically like any other DOM (e.g. XML DOM). If you had children a,b,c to the children collection of an activity in the order b,c,a, then this is the order in which they will execute. The same case for activities with branches, etc. You are basically defining the structure with the order in which you add objects, and the parent/child relationships you give them.
If you want to see a good example of what the OM code looks like for a complex workflow, open the VS designer and create a workflow of your choice. Then open the .Designer.cs file and check out the code generated inside the InitializeComponent() method. This is precisely the code you want to create to define your workflow.
Please let me know if you need further details on this.
Thanks!
Angel