Reputation: 8942
I am creating Workflow application with persistent behaviour and two bookmarks.
I am unable to persist workflow runtime for the second bookmark.
Workflow statement
1- Start workflow
2- Ask user to enter name
3- Create bookmark
4- Resume bookmark on receiving input from user and show it on UI
5- Ask user again to enter number
6- Create bookmark and wait for user input // Unable to persist at this point
7- Resume bookmark on receiving input from user and show it on UI
Custom Activity
public class WaitForInput<TResult> : NativeActivity<TResult>
{
[RequiredArgument]
public InArgument<string> BookmarkName { get; set; }
// indicate to the runtime that this activity can go idle
protected override bool CanInduceIdle
{
get { return true; }
}
protected override void Execute(NativeActivityContext context)
{
context.CreateBookmark(this.BookmarkName.Get(context), new BookmarkCallback(OnReadComplete));
}
void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)
{
this.Result.Set(context, (TResult)state);
}
}
Program.cs
class Program
{
static AutoResetEvent syncEvent = new AutoResetEvent(false);
static Guid id;
static void Main(string[] args)
{
WorkflowApplication app = new WorkflowApplication(new Sequence1());
InstanceStore store = new SqlWorkflowInstanceStore(@"Data Source=.\SQLEXPRESS;Initial Catalog=WF45GettingStartedTutorial;Integrated Security=True");
InstanceHandle handle = store.CreateInstanceHandle();
InstanceView view = store.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
handle.Free();
store.DefaultInstanceOwner = view.InstanceOwner;
app.InstanceStore = store;
app.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
{
return PersistableIdleAction.Unload;
};
app.Unloaded = delegate(WorkflowApplicationEventArgs e)
{
syncEvent.Set();
};
app.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
{
Console.WriteLine("Workflow {0} Completed.", e.InstanceId);
};
id = app.Id;
app.Run();
syncEvent.WaitOne();
string text = Console.ReadLine();
app = new WorkflowApplication(new Sequence1());
app.InstanceStore = store;
app.Completed = (workflowApplicationCompletedEventArgs) =>
{
Console.WriteLine("WF Bookmark1 has Completed in the {0} state.",
workflowApplicationCompletedEventArgs.CompletionState);
};
app.Unloaded = (workflowApplicationEventArgs) =>
{
Console.WriteLine("WF Bookmark1 unloaded");
syncEvent.Set();
};
app.Load(id);
app.ResumeBookmark("readText", text);
syncEvent.WaitOne();
// resume bookmark 2
int number = ReadNumberFromConsole();
app = new WorkflowApplication(new Sequence1());
app.InstanceStore = store;
app.Completed = (workflowApplicationCompletedEventArgs) =>
{
Console.WriteLine("WF Bookmark2 has Completed in the {0} state.",
workflowApplicationCompletedEventArgs.CompletionState);
};
app.Unloaded = (workflowApplicationEventArgs) =>
{
Console.WriteLine("WF Bookmark1 unloaded");
syncEvent.Set();
};
app.Load(id);
app.ResumeBookmark("readNumber", number);
syncEvent.WaitOne();
Console.ReadLine();
}
}
Problem
Workflow Runtime ask user to enter name and create bookmark and calls PersistableIdleAction.Unload
After i entered username on console, it reloads the workflow instance and resume bookmark.
It does not call PersistableIdleAction.Unload
for next activity.
Kindly help
Upvotes: 1
Views: 166
Reputation: 8942
Problem was with multi threading concept
here.
app.Run();
Above line start workflow runtime on new thread since i am invoking it using WorkflowApplication
(and not WorkflowInvoker
).
While creating 1st bookmark, workflow was persisted & unloaded from this thread.
I was creating new workflow runtime when resuming above bookmark and so it has different thread.
Solution: I should persist & unload workflow from this thread and not from the 1st first thread.
I am learning it so might be wrong somewhere but honestly this is what i understood after spending a lot on it.
The correct version of Program.cs
namespace Microsoft.Samples.Activities.Statements
{
class Program
{
static AutoResetEvent syncEvent = new AutoResetEvent(false);
static Guid id;
static void Main(string[] args)
{
// create the workflow app and add handlers for the Idle and Completed actions
WorkflowApplication app = new WorkflowApplication(new Sequence1());
//setup persistence
InstanceStore store = new SqlWorkflowInstanceStore(@"Data Source=.\SQLEXPRESS;Initial Catalog=WF45GettingStartedTutorial;Integrated Security=True");
InstanceHandle handle = store.CreateInstanceHandle();
InstanceView view = store.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
handle.Free();
store.DefaultInstanceOwner = view.InstanceOwner;
app.InstanceStore = store;
app.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
{
syncEvent.Set();
return PersistableIdleAction.Unload;
};
app.Unloaded = delegate(WorkflowApplicationEventArgs e)
{
syncEvent.Set();
};
app.Completed = delegate(WorkflowApplicationCompletedEventArgs e)
{
Console.WriteLine("Workflow {0} Completed.", e.InstanceId);
syncEvent.Set();
};
// start the application
id = app.Id;
app.Run();
syncEvent.WaitOne();
// resume bookmark 1
string text = Console.ReadLine();
app = new WorkflowApplication(new Sequence1());
app.InstanceStore = store;
app.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e)
{
syncEvent.Set();
return PersistableIdleAction.Unload;
};
app.Completed = (workflowApplicationCompletedEventArgs) =>
{
Console.WriteLine("WF Bookmark1 has Completed in the {0} state.",
workflowApplicationCompletedEventArgs.CompletionState);
syncEvent.Set();
};
app.Unloaded = (workflowApplicationEventArgs) =>
{
Console.WriteLine("WF Bookmark1 unloaded");
syncEvent.Set();
};
app.Load(id);
app.ResumeBookmark("readText", text);
syncEvent.WaitOne();
// resume bookmark 2
int number = ReadNumberFromConsole();
app = new WorkflowApplication(new Sequence1());
app.InstanceStore = store;
app.Completed = (workflowApplicationCompletedEventArgs) =>
{
Console.WriteLine("WF Bookmark2 has Completed in the {0} state.",
workflowApplicationCompletedEventArgs.CompletionState);
syncEvent.Set();
};
app.Unloaded = (workflowApplicationEventArgs) =>
{
Console.WriteLine("WF Bookmark1 unloaded");
syncEvent.Set();
};
app.Load(id);
app.ResumeBookmark("readNumber", number);
syncEvent.WaitOne();
Console.WriteLine("");
Console.WriteLine("Press [ENTER] to exit...");
Console.ReadLine();
}
}
}
Feel free to correct my point here. Thanks
Upvotes: 0