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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
namespace System.Workflow.Activities
{
using System;
using System.Text;
using System.Reflection;
using System.Collections;
using System.CodeDom;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Collections.Generic;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.ComponentModel.Compiler;
using System.Diagnostics;
using System.Globalization;
using System.Workflow.Activities.Common;
[SRDescription(SR.DelayActivityDescription)]
[ToolboxItem(typeof(ActivityToolboxItem))]
[Designer(typeof(DelayDesigner), typeof(IDesigner))]
[ToolboxBitmap(typeof(DelayActivity), "Resources.Delay.png")]
[DefaultEvent("InitializeTimeoutDuration")]
[ActivityValidator(typeof(DelayActivityValidator))]
[SRCategory(SR.Standard)]
[Obsolete("The System.Workflow.* types are deprecated. Instead, please use the new types from System.Activities.*")]
public sealed class DelayActivity : Activity, IEventActivity, IActivityEventListener<QueueEventArgs>
{
#region Public Dependency Properties
public static readonly DependencyProperty InitializeTimeoutDurationEvent = DependencyProperty.Register("InitializeTimeoutDuration", typeof(EventHandler), typeof(DelayActivity));
public static readonly DependencyProperty TimeoutDurationProperty = DependencyProperty.Register("TimeoutDuration", typeof(TimeSpan), typeof(DelayActivity), new PropertyMetadata(new TimeSpan(0, 0, 0)));
#endregion
#region Private Dependency Properties
private static readonly DependencyProperty QueueNameProperty = DependencyProperty.Register("QueueName", typeof(IComparable), typeof(DelayActivity));
#endregion
#region Constructors
public DelayActivity()
{
}
public DelayActivity(string name)
: base(name)
{
}
#endregion
#region Public Handlers
[SRCategory(SR.Handlers)]
[SRDescription(SR.TimeoutInitializerDescription)]
[MergableProperty(false)]
public event EventHandler InitializeTimeoutDuration
{
add
{
base.AddHandler(InitializeTimeoutDurationEvent, value);
}
remove
{
base.RemoveHandler(InitializeTimeoutDurationEvent, value);
}
}
#endregion
#region Public Properties
[SRDescription(SR.TimeoutDurationDescription)]
[MergableProperty(false)]
[TypeConverter(typeof(TimeoutDurationConverter))]
public TimeSpan TimeoutDuration
{
get
{
return (TimeSpan)base.GetValue(TimeoutDurationProperty);
}
set
{
base.SetValue(TimeoutDurationProperty, value);
}
}
#endregion
#region Protected Methods
protected override void Initialize(IServiceProvider provider)
{
base.Initialize(provider);
// Define the queue name for this Delay
this.SetValue(QueueNameProperty, Guid.NewGuid());
}
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
if (executionContext == null)
throw new ArgumentNullException("executionContext");
if (this.IsInEventActivityMode)
{
return ActivityExecutionStatus.Closed;
}
else
{
((IEventActivity)this).Subscribe(executionContext, this);
this.IsInEventActivityMode = false;
return ActivityExecutionStatus.Executing;
}
}
protected override ActivityExecutionStatus Cancel(ActivityExecutionContext executionContext)
{
if (executionContext == null)
throw new ArgumentNullException("executionContext");
if (!this.IsInEventActivityMode)
{
if (this.SubscriptionID != Guid.Empty)
{
((IEventActivity)this).Unsubscribe(executionContext, this);
}
}
return ActivityExecutionStatus.Closed;
}
protected sealed override ActivityExecutionStatus HandleFault(ActivityExecutionContext executionContext, Exception exception)
{
if (executionContext == null)
throw new ArgumentNullException("executionContext");
if (exception == null)
throw new ArgumentNullException("exception");
ActivityExecutionStatus newStatus = this.Cancel(executionContext);
if (newStatus == ActivityExecutionStatus.Canceling)
return ActivityExecutionStatus.Faulting;
return newStatus;
}
protected override void OnClosed(IServiceProvider provider)
{
base.RemoveProperty(DelayActivity.SubscriptionIDProperty);
base.RemoveProperty(DelayActivity.IsInEventActivityModeProperty);
}
private class DelayActivityValidator : ActivityValidator
{
public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
{
ValidationErrorCollection errors = new ValidationErrorCollection();
DelayActivity delay = obj as DelayActivity;
if (delay == null)
throw new InvalidOperationException();
if (delay.TimeoutDuration.Ticks < 0)
{
errors.Add(new ValidationError(SR.GetString(SR.Error_NegativeValue, new object[] { delay.TimeoutDuration.ToString(), "TimeoutDuration" }), ErrorNumbers.Error_NegativeValue));
}
errors.AddRange(base.Validate(manager, obj));
return errors;
}
}
#endregion
#region Private Implementation
#region Runtime Data / Properties
static DependencyProperty SubscriptionIDProperty = DependencyProperty.Register("SubscriptionID", typeof(Guid), typeof(DelayActivity), new PropertyMetadata(Guid.NewGuid()));
static DependencyProperty IsInEventActivityModeProperty = DependencyProperty.Register("IsInEventActivityMode", typeof(bool), typeof(DelayActivity), new PropertyMetadata(false));
private Guid SubscriptionID
{
get
{
return (Guid)base.GetValue(DelayActivity.SubscriptionIDProperty);
}
set
{
base.SetValue(DelayActivity.SubscriptionIDProperty, value);
}
}
private bool IsInEventActivityMode
{
get
{
return (bool)base.GetValue(DelayActivity.IsInEventActivityModeProperty);
}
set
{
base.SetValue(DelayActivity.IsInEventActivityModeProperty, value);
}
}
#endregion
#region Timeout Duration Conversion Helper
private sealed class TimeoutDurationConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is TimeSpan)
{
TimeSpan timespan = (TimeSpan)value;
return timespan.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
object parsedTimespan = TimeSpan.Zero;
string timeSpan = value as string;
if (!String.IsNullOrEmpty(timeSpan))
{
//If this fails then an exception is thrown and the property set would fail
try
{
parsedTimespan = TimeSpan.Parse(timeSpan, CultureInfo.InvariantCulture);
if (parsedTimespan != null && ((TimeSpan)parsedTimespan).Ticks < 0)
throw new Exception(string.Format(System.Globalization.CultureInfo.CurrentCulture, SR.GetString(SR.Error_NegativeValue), value.ToString(), "TimeoutDuration"));
}
catch
{
throw new Exception(SR.GetString(SR.InvalidTimespanFormat, timeSpan));
}
}
return parsedTimespan;
}
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
ArrayList standardValuesCollection = new ArrayList();
standardValuesCollection.Add(new TimeSpan(0, 0, 0));
standardValuesCollection.Add(new TimeSpan(0, 1, 0));
standardValuesCollection.Add(new TimeSpan(0, 30, 0));
standardValuesCollection.Add(new TimeSpan(1, 0, 0));
standardValuesCollection.Add(new TimeSpan(12, 0, 0));
standardValuesCollection.Add(new TimeSpan(1, 0, 0, 0));
return new StandardValuesCollection(standardValuesCollection);
}
}
#endregion
#endregion
#region IActivityEventListener<QueueEventArgs> Implementation
void IActivityEventListener<QueueEventArgs>.OnEvent(object sender, QueueEventArgs e)
{
if (sender == null)
throw new ArgumentNullException("sender");
if (e == null)
throw new ArgumentNullException("e");
ActivityExecutionContext context = sender as ActivityExecutionContext;
if (context == null)
throw new ArgumentException(SR.Error_SenderMustBeActivityExecutionContext, "sender");
if (this.ExecutionStatus != ActivityExecutionStatus.Closed)
{
System.Diagnostics.Debug.Assert(this.SubscriptionID != Guid.Empty);
WorkflowQueuingService qService = context.GetService<WorkflowQueuingService>();
qService.GetWorkflowQueue(e.QueueName).Dequeue();
qService.DeleteWorkflowQueue(e.QueueName);
context.CloseActivity();
}
}
#endregion
#region IEventActivity implementation
void IEventActivity.Subscribe(ActivityExecutionContext parentContext, IActivityEventListener<QueueEventArgs> parentEventHandler)
{
if (parentContext == null)
throw new ArgumentNullException("parentContext");
if (parentEventHandler == null)
throw new ArgumentNullException("parentEventHandler");
this.IsInEventActivityMode = true;
base.RaiseEvent(DelayActivity.InitializeTimeoutDurationEvent, this, EventArgs.Empty);
TimeSpan timeSpan = this.TimeoutDuration;
DateTime timeOut = DateTime.UtcNow + timeSpan;
WorkflowQueuingService qService = parentContext.GetService<WorkflowQueuingService>();
IComparable queueName = ((IEventActivity)this).QueueName;
TimerEventSubscription timerSub = new TimerEventSubscription((Guid)queueName, this.WorkflowInstanceId, timeOut);
WorkflowQueue queue = qService.CreateWorkflowQueue(queueName, false);
queue.RegisterForQueueItemAvailable(parentEventHandler, this.QualifiedName);
this.SubscriptionID = timerSub.SubscriptionId;
Activity root = this;
while (root.Parent != null)
root = root.Parent;
TimerEventSubscriptionCollection timers = (TimerEventSubscriptionCollection)root.GetValue(TimerEventSubscriptionCollection.TimerCollectionProperty);
Debug.Assert(timers != null, "TimerEventSubscriptionCollection on root activity should never be null, but it was");
timers.Add(timerSub);
}
void IEventActivity.Unsubscribe(ActivityExecutionContext parentContext, IActivityEventListener<QueueEventArgs> parentEventHandler)
{
if (parentContext == null)
throw new ArgumentNullException("parentContext");
if (parentEventHandler == null)
throw new ArgumentNullException("parentEventHandler");
System.Diagnostics.Debug.Assert(this.SubscriptionID != Guid.Empty);
WorkflowQueuingService qService = parentContext.GetService<WorkflowQueuingService>();
WorkflowQueue wfQueue = null;
try
{
wfQueue = qService.GetWorkflowQueue(this.SubscriptionID);
}
catch
{
// If the queue no longer exists, eat the exception, we clear the subscription id later.
}
if (wfQueue != null && wfQueue.Count != 0)
wfQueue.Dequeue();
// WinOE Bug 16929: In the case of dynamic update, if this activity is being removed,
// we can not trace back to the root activity from "this".
Activity root = parentContext.Activity;
while (root.Parent != null)
root = root.Parent;
TimerEventSubscriptionCollection timers = (TimerEventSubscriptionCollection)root.GetValue(TimerEventSubscriptionCollection.TimerCollectionProperty);
Debug.Assert(timers != null, "TimerEventSubscriptionCollection on root activity should never be null, but it was");
timers.Remove(this.SubscriptionID);
if (wfQueue != null)
{
wfQueue.UnregisterForQueueItemAvailable(parentEventHandler);
qService.DeleteWorkflowQueue(this.SubscriptionID);
}
this.SubscriptionID = Guid.Empty;
}
IComparable IEventActivity.QueueName
{
get
{
return (IComparable)this.GetValue(QueueNameProperty);
}
}
#endregion
}
}
|