Foundations
ActivityExecutionContext in Workflows
Matt Milner
Navigate:
[root] /
MSDNMag.WFActivities /
ReadLine.cs
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
namespace MSDNMag.WFActivities
{
[Designer(typeof(ConsoleDesigner), typeof(IDesigner))]
[ToolboxBitmap(typeof(ReadLine), "Resources.icon_exe.png")]
public partial class ReadLine : System.Workflow.ComponentModel.Activity, IActivityEventListener<QueueEventArgs>, IEventActivity
{
private string qName;
public ReadLine()
{
InitializeComponent();
}
public static DependencyProperty TextProperty = System.Workflow.ComponentModel.DependencyProperty.Register("Text", typeof(string), typeof(ReadLine));
[Description("The text read in from the console")]
[Category("Console")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string Text
{
get
{
return ((string)(base.GetValue(ReadLine.TextProperty)));
}
set
{
base.SetValue(ReadLine.TextProperty, value);
}
}
protected override void Initialize(IServiceProvider provider)
{
base.Initialize(provider);
qName = this.QualifiedName;
}
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
if (ProcessMessage(executionContext))
{
return ActivityExecutionStatus.Closed;
}
else
{
Subscribe(executionContext, this);
return ActivityExecutionStatus.Executing;
}
}
private bool ProcessMessage(ActivityExecutionContext provider)
{
WorkflowQueuingService qService = provider.GetService<WorkflowQueuingService>();
if (qService.Exists(this.QueueName) && qService.GetWorkflowQueue(this.QueueName).Count > 0)
{
object input = qService.GetWorkflowQueue(this.QueueName).Dequeue();
if (input == null)
return false;
else
{
Text = input.ToString();
return true;
}
}
return false;
}
#region IActivityEventListener<QueueEventArgs> Members
public void OnEvent(object sender, QueueEventArgs e)
{
ActivityExecutionContext ctx = sender as ActivityExecutionContext;
if (ProcessMessage(ctx))
{
Unsubscribe(ctx, this);
ctx.CloseActivity();
}
}
#endregion
protected override void Uninitialize(IServiceProvider provider)
{
base.Uninitialize(provider);
}
#region IEventActivity Members
public IComparable QueueName
{
get { return qName; }
}
public void Subscribe(ActivityExecutionContext parentContext, IActivityEventListener<QueueEventArgs> parentEventHandler)
{
WorkflowQueuingService qService = parentContext.GetService<WorkflowQueuingService>();
if (qService != null && !qService.Exists(this.QueueName))
{
WorkflowQueue q= qService.CreateWorkflowQueue(this.QueueName, false);
q.RegisterForQueueItemAvailable(parentEventHandler);
IReadLineService readService = parentContext.GetService<IReadLineService>();
if (readService != null)
readService.ReadLine(this.QueueName);
}
}
public void Unsubscribe(ActivityExecutionContext parentContext, IActivityEventListener<QueueEventArgs> parentEventHandler)
{
WorkflowQueuingService qService = parentContext.GetService<WorkflowQueuingService>();
if (qService != null && qService.Exists(this.QueueName))
{
qService.GetWorkflowQueue(this.QueueName).UnregisterForQueueItemAvailable(parentEventHandler);
qService.DeleteWorkflowQueue(this.QueueName);
}
}
#endregion
}
}