1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
#region Using directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.Remoting.Messaging;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
#endregion Using directives
namespace System.Workflow.Activities
{
#region StateMachineSubscription
[Serializable]
internal abstract class StateMachineSubscription : IActivityEventListener<QueueEventArgs>
{
#region Member Variables
private Guid _subscriptionId;
#endregion Member Variables
#region Properties
internal Guid SubscriptionId
{
get
{
return _subscriptionId;
}
set
{
_subscriptionId = value;
}
}
#endregion Properties
void IActivityEventListener<QueueEventArgs>.OnEvent(object sender, QueueEventArgs e)
{
ActivityExecutionContext context = sender as ActivityExecutionContext;
if (context == null)
throw new ArgumentException(SR.Error_SenderMustBeActivityExecutionContext, "sender");
Enqueue(context);
}
protected abstract void Enqueue(ActivityExecutionContext context);
internal abstract void ProcessEvent(ActivityExecutionContext context);
}
#endregion StateMachineSubscription
#region EventActivitySubscription
[Serializable]
internal class EventActivitySubscription : StateMachineSubscription
{
#region Member Variables
private string _eventActivityName = String.Empty;
private string _eventDrivenName = String.Empty;
private string _stateName = String.Empty;
private IComparable _queueName;
#endregion Member Variables
#region Properties
internal string EventActivityName
{
get
{
return _eventActivityName;
}
}
internal string StateName
{
get
{
return _stateName;
}
}
internal IComparable QueueName
{
get
{
return _queueName;
}
}
internal string EventDrivenName
{
get
{
return _eventDrivenName;
}
}
#endregion Properties
internal void Subscribe(ActivityExecutionContext context,
StateActivity state,
IEventActivity eventActivity)
{
eventActivity.Subscribe(context, this);
Activity activity = (Activity)eventActivity;
this._queueName = eventActivity.QueueName;
this._eventActivityName = activity.QualifiedName;
this._stateName = state.QualifiedName;
this.SubscriptionId = Guid.NewGuid();
EventDrivenActivity eventDriven = StateMachineHelpers.GetParentEventDriven(eventActivity);
this._eventDrivenName = eventDriven.QualifiedName;
}
internal void Unsubscribe(ActivityExecutionContext context,
IEventActivity eventActivity)
{
eventActivity.Unsubscribe(context, this);
}
protected override void Enqueue(ActivityExecutionContext context)
{
StateActivity rootState = StateMachineHelpers.GetRootState((StateActivity)context.Activity);
StateMachineExecutionState executionState = StateMachineExecutionState.Get(rootState);
executionState.SubscriptionManager.Enqueue(context, this.QueueName);
}
internal override void ProcessEvent(ActivityExecutionContext context)
{
StateActivity rootState = StateMachineHelpers.GetRootState((StateActivity)context.Activity);
StateMachineExecutionState executionState = StateMachineExecutionState.Get(rootState);
ExternalEventAction action = new ExternalEventAction(this.StateName, this.EventDrivenName);
Debug.Assert(!executionState.HasEnqueuedActions);
executionState.EnqueueAction(action);
executionState.ProcessActions(context);
}
}
#endregion EventActivitySubscription
#region SetStateSubscription
[Serializable]
internal class SetStateSubscription : StateMachineSubscription
{
private Guid _instanceId;
internal SetStateSubscription(Guid instanceId)
{
this._instanceId = instanceId;
}
internal void CreateQueue(ActivityExecutionContext context)
{
if (!StateMachineHelpers.IsRootExecutionContext(context))
{
// we only subscribe to the set state event if
// we're at the root level. If this instance is
// being called, it is not possible to set the
// state from the host side directly
return;
}
WorkflowQueuingService workflowQueuingService = context.GetService<WorkflowQueuingService>();
MessageEventSubscription subscription = new MessageEventSubscription(
StateMachineWorkflowActivity.SetStateQueueName,
this._instanceId);
WorkflowQueue workflowQueue = workflowQueuingService.CreateWorkflowQueue(
StateMachineWorkflowActivity.SetStateQueueName,
true);
this.SubscriptionId = subscription.SubscriptionId;
}
internal void DeleteQueue(ActivityExecutionContext context)
{
if (!StateMachineHelpers.IsRootExecutionContext(context))
{
// we only subscribe to the set state event if
// we're at the root level. If this instance is
// being called, it is not possible to set the
// state from the host side directly
return;
}
WorkflowQueuingService workflowQueuingService = context.GetService<WorkflowQueuingService>();
WorkflowQueue workflowQueue = workflowQueuingService.GetWorkflowQueue(StateMachineWorkflowActivity.SetStateQueueName);
workflowQueuingService.DeleteWorkflowQueue(StateMachineWorkflowActivity.SetStateQueueName);
}
internal void Subscribe(ActivityExecutionContext context)
{
WorkflowQueuingService workflowQueuingService = context.GetService<WorkflowQueuingService>();
WorkflowQueue workflowQueue = workflowQueuingService.GetWorkflowQueue(StateMachineWorkflowActivity.SetStateQueueName);
workflowQueue.RegisterForQueueItemAvailable(this);
}
internal void Unsubscribe(ActivityExecutionContext context)
{
WorkflowQueuingService workflowQueuingService = context.GetService<WorkflowQueuingService>();
WorkflowQueue workflowQueue = workflowQueuingService.GetWorkflowQueue(StateMachineWorkflowActivity.SetStateQueueName);
workflowQueue.UnregisterForQueueItemAvailable(this);
}
protected override void Enqueue(ActivityExecutionContext context)
{
StateActivity rootState = StateMachineHelpers.GetRootState((StateActivity)context.Activity);
StateMachineExecutionState executionState = StateMachineExecutionState.Get(rootState);
executionState.SubscriptionManager.Enqueue(context, this.SubscriptionId);
}
internal override void ProcessEvent(ActivityExecutionContext context)
{
WorkflowQueuingService workflowQueuingService = context.GetService<WorkflowQueuingService>();
WorkflowQueue workflowQueue = workflowQueuingService.GetWorkflowQueue(StateMachineWorkflowActivity.SetStateQueueName);
SetStateEventArgs eventArgs = workflowQueue.Dequeue() as SetStateEventArgs;
StateActivity currentState = StateMachineHelpers.GetCurrentState(context);
if (currentState == null)
throw new InvalidOperationException(SR.GetStateMachineWorkflowMustHaveACurrentState());
StateActivity rootState = StateMachineHelpers.GetRootState((StateActivity)context.Activity);
StateMachineExecutionState executionState = StateMachineExecutionState.Get(rootState);
SetStateAction action = new SetStateAction(currentState.QualifiedName, eventArgs.TargetStateName);
Debug.Assert(!executionState.HasEnqueuedActions);
executionState.EnqueueAction(action);
executionState.ProcessActions(context);
}
}
#endregion SetStateSubscription
}
|