To
this point we have focused on creating an activity and invoking it
in the simplest way possible with the WorkflowInvoker
class. The WorkflowInvoker.Invoke
method is simple because it is synchronous and invokes the workflow on the same
thread as the caller.
We will modify the host application to run your SayHello activity with
WorkflowApplication and observe the threading behavior. Therefore we now have
two requirements for our workflow:
1. Return a personalized greeting
2. Return a non-zero Int32 value containing the managed thread ID
that the workflow was invoked on
Using
the “write the test first” approach we will begin by writing a test that
verifies our new requirement for the workflow thread ID.
- First open HelloWorkFlow project from last post Testing Workflows .
- Open
SayHelloFixture.cs and add the following namespace directivesC#using System.Threading;using System.Diagnostics;Visual BasicImports System.Threading
- Add
the ShouldReturnWorkflowThread test
as shownC#/// <summary>/// Verifies that the workflow returns an Out Argument/// Name: WorkflowThread/// Type: Int32/// Value: Non-Zero/// </summary>[TestMethod]public void ShouldReturnWorkflowThread(){var output = WorkflowInvoker.Invoke(new SayHello(){UserName = "Test"});Assert.IsTrue(output.ContainsKey("WorkflowThread"),"SayHello must contain an OutArgument named WorkflowThread");// Don't know for sure what it is yetvar outarg = output["WorkflowThread"];Assert.IsInstanceOfType(outarg, typeof(Int32),"WorkflowThread must be of type Int32");Assert.AreNotEqual(0, outarg,"WorkflowThread must not be zero");Debug.WriteLine("Test thread is " +Thread.CurrentThread.ManagedThreadId);Debug.WriteLine("Workflow thread is " + outarg.ToString());}Visual Basic''' <summary>''' Verifies that the SayHello workflow contains an Out Argument''' Name: WorkflowThread''' Type: Int32''' Value: Non-Zero''' </summary>''' <remarks></remarks><TestMethod()> Public Sub ShouldReturnWorkflowThread()Dim output = WorkflowInvoker.Invoke( _New SayHello() With {.UserName = "Test"})Assert.IsTrue(output.ContainsKey("WorkflowThread"),"SayHello must contain an OutArgument named WorkflowThread")' Don't know for sure what it is yetDim outarg = output("WorkflowThread")Assert.IsInstanceOfType(outarg, GetType(Int32),"WorkflowThread must be of type Int32")Assert.AreNotEqual(0, output("WorkflowThread"),"WorkflowThread must not be zero")Debug.WriteLine("Test thread is " & _Thread.CurrentThread.ManagedThreadId)Debug.WriteLine("Workflow thread is " & outarg.ToString())End Sub
- Press CTRL + SHIFT + B to rebuild the application.
- Press
CTRL+R, T to run all tests in the
current context. You should see the test fail because you have not added the
WorkflowThread out argument yet.One test passes, one test fails because you have no WorkflowThread argument
- Add
an Out argument named WorkflowThread of type Int32Add the WorkflowThread Out argumentUntil now our workflow has been just one activity. But now we need two activities, one to assign the greeting and another to assign the workflow thread. We need to modify the workflow to use an activity that will contain the two assign activities and there are several activities we could choose from to do this, but let’s begin with the simplest one, the Sequence activity.
- We cannot drop a sequence on the designer until we remove the Assign activity
that is already there. We will use this
assign activity inside the sequence so we need to cut it, drop the sequence
and then and paste it back. Right click on the Assign activity and select Cut.Cutting an Activity
- Drag
a Sequence and drop it on the design
surfaceDrop a Sequence on the designer
- Right
click inside the sequence and select Paste
to put the Assign activity inside the sequence.Paste the assign activity inside the sequence
- Import
the System.Threading namespace into your
workflow. Click on Imports and add System.ThreadingClick on Imports and add System.ThreadingNo, this is optional just as it would be in any C# or VB project. If you don’t import the namespace you will have to fully qualify classes from the name space as in System.Threading.Thread
- Now
we need to assign the current managed thread ID to the WorkflowThread out argument. Drop an Assign activity on the
Sequence below the first Assign activity and set the properties as shownSet the properties of the second Assign activity as shown
- Press CTRL + SHIFT + B to rebuild the application.
- Press
CTRL+R, T to run tests in current
context. The tests will now pass. If we want to see the thread returned by the
workflow, double click on the ShouldReturnWorkflowThread
test. The Debug output will appear in
the test results. The actual thread ID will vary on your computer but we will
notice that the thread ID for the test is the same as the thread ID for the
Workflow because WorkflowInvoker
invokes the workflow synchronously on the calling thread.Debug trace output appears in test results
No comments:
Post a Comment