This article will introduce you to the related applications of Windows Workflow 4.0 in Visual Studio 2010. I hope this short article can help you develop Workflow 4.0.
Recently installed Visual Studio 2010 in a virtual machine. The interface is WPF, and the CPU and memory usage are not exaggerated. When you open a very simple attached Lab Project, the CPU usage is generally less than 20%, and the memory usage is less than 800M.
Closer to home, let’s introduce Windows Workflow 4.0.
The workflow model has changed a lot compared to 3.5.
We know that workflows in 3.5 are hosted in WorkflowRuntime, and workflow instances are created and executed through WorkflowRuntime; there is no WorkflowRuntime class in 4.0, so you can easily create WorkflowInstance instances and execute workflows directly. The code in Lab is as follows:
WorkflowInstance myInstance = new WorkflowInstance(new SayHello(),
new SayHelloInArgs(userName));
myInstance.OnCompleted = delegate(WorkflowCompletedEventArgs e)
{
Console.WriteLine("*** OnCompleted delegate is running on thread {0} ***",
Thread.CurrentThread.ManagedThreadId);
SayHelloOutArgs outArgs = new SayHelloOutArgs(e.Outputs);
greeting = outArgs.Greeting;
syncEvent.Set();
};
myInstance.OnUnhandledException = delegate(WorkflowUnhandledExceptionEventArgs e)
{
Console.WriteLine(e.UnhandledException.ToString());
return UnhandledExceptionAction.Terminate;
};
myInstance.OnAborted = delegate(WorkflowAbortedEventArgs e)
{
Console.WriteLine(e.Reason);
syncEvent.Set();
};
myInstance.Run();
There is a WorkflowInvoker class in 4.0. This class can also execute workflows, but this class is used to test workflows. This greatly improves the difficulty of testing workflows in the previous version.
[TestMethod]
public void ShouldReturnGreetingWithName()
{
Dictionary<string, object> input = new Dictionary
<string, object>()
{
{"UserName", "Test"}
};
IDictionary<string, object> output;
output = WorkflowInvoker.Invoke(new SayHello(), input);
Assert.AreEqual("Hello, Test from Workflow 4", output["Greeting"]);
}
Activity in 3.5 is the base class of all activities. To implement custom activities, you only need to override the Execute() method of Activity; in 4.0, all activities are derived from the abstract class WorkflowElement, and are customized by default in Visual Studio. Activities are inherited from CodeActivity or CodeActivity<T>. Similarly, the Execute() method also needs to be rewritten to implement custom execution logic.
public class MyActivity1 : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
//your implementation code
}
}
Of course, you can still derive custom activities from Activity, but it is very different from 3.5.
public class SayHelloInCode : Activity
{
protected override WorkflowElement CreateBody()
{
return new Sequence()
{
Activities =
{
newWriteLine()
{
Text = "Hello Workflow 4 in code"
}
}
};
}
}
The newly added workflow service function in 4.0 can directly publish the workflow as a WCF service. Of course, the workflow must also be designed with WCF response function. 4.0 provides four WCF-related activities: Receive, ReceiveReply, Send, and SendReply. Through these activities, WCF service operations can be visually defined.
The basic model of workflow designer is implemented in 4.0, and custom designers can be easily implemented.