|
|
Goals to demonstrate:
Steps:
private void PrintMessageCode(object sender, EventArgs e)
{
Console.WriteLine("Simple Sequential Workflow Invocation");
Console.WriteLine(String.Format("Date: {0}", DateTime.Now));
Console.WriteLine(String.Format("Workflow Instance ID: {0}", WorkflowInstanceId));
}
using System;
using System.Workflow.Runtime;
namespace WorkflowInvoker
{
internal static class Hosting
{
private static WorkflowRuntime runtime;
public static WorkflowRuntime Runtime
{
get { return runtime; }
}
static Hosting()
{
StartRuntime();
}
private static void StartRuntime()
{
AppDomain.CurrentDomain.ProcessExit += StopRuntime;
AppDomain.CurrentDomain.DomainUnload += StopRuntime;
runtime = new WorkflowRuntime();
runtime.StartRuntime();
}
private static void StopRuntime(object sender, EventArgs e)
{
if (!runtime.IsStarted)
return;
try
{
runtime.StopRuntime();
}
catch (ObjectDisposedException) {}
}
public static Guid InvokeWorkflow(Type workflowType)
{
WorkflowInstance instance = runtime.CreateWorkflow(workflowType);
instance.Start();
return instance.InstanceId;
}
}
}
using System;
using System.Workflow.Runtime;
using System.Threading;
using WorkflowLibrary;
namespace WorkflowInvoker
{
public static class Invoker
{
private static readonly AutoResetEvent waitHandle;
public static AutoResetEvent WaitHandle
{
get { return waitHandle; }
}
static Invoker()
{
waitHandle = new AutoResetEvent(false);
WorkflowRuntime runtime = Hosting.Runtime;
runtime.WorkflowCompleted += WorkflowCompleted;
runtime.WorkflowTerminated += WorkflowTerminated;
runtime.WorkflowIdled += WorkflowIdled;
}
private static void WorkflowIdled(object sender, WorkflowEventArgs e)
{
Console.WriteLine("Workflow Runtime Event: Workflow instance idled");
}
private static void WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)
{
Console.WriteLine(
String.Format(
"Workflow Runtime Event: Workflow instance terminated. " +
"Reason: '{0}'", e.Exception.Message));
waitHandle.Set();
}
private static void WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
{
Console.WriteLine("Workflow Runtime Event: Workflow instance completed");
waitHandle.Set();
}
public static Guid RunSimpleWorkflow()
{
return Hosting.InvokeWorkflow(typeof (Workflow1));
}
}
}
static void Main(string[] args)
{
Invoker.RunSimpleWorkflow();
Invoker.WaitHandle.WaitOne();
Console.ReadLine();
}
private void AfterDelayCode(object sender, EventArgs e)
{
Console.WriteLine("Timer expired");
}
private void DelayInit(object sender, EventArgs e)
{
Console.WriteLine("Delay activity initialized");
Console.WriteLine(String.Format("Delay duration: {0}", DelayTimeoutDuration));
}
public static Guid InvokeWorkflow(Type workflowType, Dictionary<string, object> parameters)
{
WorkflowInstance instance = runtime.CreateWorkflow(workflowType, parameters);
instance.Start();
return instance.InstanceId;
}
public static Guid RunSimpleWorkflow(TimeSpan delayDuration)
{
Dictionary<string, object> parameters = new Dictionary<string, object> {{"DelayTimeoutDuration", delayDuration}};
return Hosting.InvokeWorkflow(typeof (Workflow1), parameters);
}
using System;
using WorkflowInvoker;
namespace WorkflowApplication
{
class Program
{
static void Main(string[] args)
{
Invoker.RunSimpleWorkflow(new TimeSpan(0, 0, 5)); // 5 seconds
Invoker.WaitHandle.WaitOne();
Console.ReadLine();
}
}
}