Customizing a MyReadLine activity with Bookmark
By using InArgument, OutArgument, and InOutArgument, we can flow data into the Workflow when it starts and out of the Workflow when it ends. But how can we pass data from the caller into the Workflow when it is executing?-Bookmark will help us to do this. In this task, we will create a MyReadLine activity using a bookmark.
How to do it...
using System.Activities;
namespace UseBookmark
{
public class MyReadLine : NativeActivity<string>
[RequiredArgument]
public InArgument<string> BookmarkName { get; set; }
protected override void Execute(
NativeActivityContext context)
context.CreateBookmark(BookmarkName.Get(context),
new BookmarkCallback(OnResumeBookmark));
}
protected override bool CanInduceIdle
get
{ return true; }
public void OnResumeBookmark(
NativeActivityContext context,
Bookmark bookmark,
object obj)
Result.Set(context, (string)obj);
using System;
using System.Linq;
using System.Activities.Statements;
using System.Threading;
namespace UseBookmark{
class Program
static void Main(string[] args)
AutoResetEvent syncEvent = new AutoResetEvent(false);
string bookmarkName = "GreetingBookmark";
WorkflowApplication wfApp =
new WorkflowApplication(new Workflow1()
BookmarkNameInArg = bookmarkName
});
wfApp.Completed = delegate(
WorkflowApplicationCompletedEventArgs e)
syncEvent.Set();
};
wfApp.Run();
wfApp.ResumeBookmark(bookmarkName,
Console.ReadLine());
syncEvent.WaitOne();
How it works...
In the code shown in the second step, we create a class inherited from NativeActivity. NativeActivity is a special abstract activity that can be used to customize complex activities; we will talk about it more in Chapter 5, Custom Activities.
By this statement, the WF context creates a Bookmark with arguments BookMarkName and BookMarkCallback. When the wfApp.ResumeBookmark method is called, the OnResumeBookmark that was defined in the Customized Activity body will be executed.
Consider the following code snippet of step 3:
wfApp.ResumeBookmark(bookmarkName, Console.ReadLine());
When this statement is executed, the OnResumeBookmark method defined in the MyReadLine activity will be called and the method will accept the value passed via Console.ReadLine().