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
|
namespace System.Workflow.Activities
{
#region Imports
using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Reflection;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Diagnostics;
using System.Drawing.Design;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.CodeDom;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Collections.Generic;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Collections.Specialized;
using System.Workflow.Activities.Common;
#endregion
// Note that the designer is associated dynamically through the type description provider
[SRDescription(SR.InvokeWorkflowActivityDescription)]
[ToolboxItem(typeof(ActivityToolboxItem))]
[ToolboxBitmap(typeof(InvokeWorkflowActivity), "Resources.Service.bmp")]
[ActivityValidator(typeof(InvokeWorkflowValidator))]
[DefaultEvent("Invoking")]
[Obsolete("The System.Workflow.* types are deprecated. Instead, please use the new types from System.Activities.*")]
public sealed class InvokeWorkflowActivity : Activity, ITypeFilterProvider
{
public static readonly DependencyProperty TargetWorkflowProperty = DependencyProperty.Register("TargetWorkflow", typeof(Type), typeof(InvokeWorkflowActivity), new PropertyMetadata(null, DependencyPropertyOptions.Metadata));
public static readonly DependencyProperty ParameterBindingsProperty = DependencyProperty.Register("ParameterBindings", typeof(WorkflowParameterBindingCollection), typeof(InvokeWorkflowActivity), new PropertyMetadata(DependencyPropertyOptions.Metadata | DependencyPropertyOptions.ReadOnly, new Attribute[] { new BrowsableAttribute(false), new DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content) }));
public static readonly DependencyProperty InvokingEvent = DependencyProperty.Register("Invoking", typeof(EventHandler), typeof(InvokeWorkflowActivity));
public static readonly DependencyProperty InstanceIdProperty = DependencyProperty.Register("InstanceId", typeof(Guid), typeof(InvokeWorkflowActivity), new PropertyMetadata(Guid.Empty));
internal static readonly ArrayList ReservedParameterNames = new ArrayList(new string[] { "Name", "Enabled", "Description", "TargetWorkflow", "Invoking", "ParameterBindings" });
#region Constructors
public InvokeWorkflowActivity()
{
//
base.SetReadOnlyPropertyValue(ParameterBindingsProperty, new WorkflowParameterBindingCollection(this));
}
public InvokeWorkflowActivity(string name)
: base(name)
{
//
base.SetReadOnlyPropertyValue(ParameterBindingsProperty, new WorkflowParameterBindingCollection(this));
}
#endregion
[SRCategory(SR.Activity)]
[SRDescription(SR.TargetWorkflowDescr)]
[Editor(typeof(TypeBrowserEditor), typeof(UITypeEditor))]
[DefaultValue(null)]
public Type TargetWorkflow
{
get
{
return base.GetValue(TargetWorkflowProperty) as Type;
}
set
{
base.SetValue(TargetWorkflowProperty, value);
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Guid InstanceId
{
get
{
return (Guid)base.GetValue(InstanceIdProperty);
}
}
internal void SetInstanceId(Guid value)
{
//
base.SetValue(InstanceIdProperty, value);
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Browsable(false)]
public WorkflowParameterBindingCollection ParameterBindings
{
get
{
return base.GetValue(ParameterBindingsProperty) as WorkflowParameterBindingCollection;
}
}
[SRCategory(SR.Handlers)]
[SRDescription(SR.InitializeCaleeDescr)]
[MergableProperty(false)]
public event EventHandler Invoking
{
add
{
base.AddHandler(InvokingEvent, value);
}
remove
{
base.RemoveHandler(InvokingEvent, value);
}
}
#region ITypeFilterProvider Members
bool ITypeFilterProvider.CanFilterType(Type type, bool throwOnError)
{
if (type == null)
throw new ArgumentNullException("type");
bool canFilterType = TypeProvider.IsAssignable(typeof(Activity), type) && type != typeof(Activity) && !type.IsAbstract;
if (canFilterType)
{
//This means that the type is DesignTimeType
IDesignerHost designerHost = ((IComponent)this).Site.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (designerHost != null && String.Compare(designerHost.RootComponentClassName, type.FullName, StringComparison.Ordinal) == 0)
{
if (throwOnError)
throw new InvalidOperationException(SR.GetString(SR.Error_CantInvokeSelf));
else
canFilterType = false;
}
}
if (throwOnError && !canFilterType)
throw new Exception(SR.GetString(SR.Error_TypeIsNotRootActivity, "TargetWorkflow"));
return canFilterType;
}
string ITypeFilterProvider.FilterDescription
{
get
{
return SR.GetString(SR.FilterDescription_InvokeWorkflow);
}
}
#endregion
#region Execute
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
if (executionContext == null)
throw new ArgumentNullException("executionContext");
// raise event
base.RaiseEvent(InvokeWorkflowActivity.InvokingEvent, this, EventArgs.Empty);
// collect the [in] parameters
Dictionary<string, object> namedArgumentValues = new Dictionary<string, object>();
foreach (WorkflowParameterBinding paramBinding in this.ParameterBindings)
namedArgumentValues.Add(paramBinding.ParameterName, paramBinding.Value);
IStartWorkflow workflowInvoker = executionContext.GetService(typeof(IStartWorkflow)) as IStartWorkflow;
if (workflowInvoker == null)
throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(IStartWorkflow).FullName));
Guid instanceId = workflowInvoker.StartWorkflow(this.TargetWorkflow, namedArgumentValues);
if (instanceId == Guid.Empty)
throw new InvalidOperationException(SR.GetString(SR.Error_FailedToStartTheWorkflow));
this.SetInstanceId(instanceId);
return ActivityExecutionStatus.Closed;
}
#endregion
}
#region validator
internal sealed class InvokeWorkflowValidator : ActivityValidator
{
public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
{
ValidationErrorCollection validationErrors = base.Validate(manager, obj);
InvokeWorkflowActivity invokeWorkflow = obj as InvokeWorkflowActivity;
if (invokeWorkflow == null)
throw new ArgumentException(SR.GetString(SR.Error_UnexpectedArgumentType, typeof(InvokeWorkflowActivity).FullName), "obj");
if (invokeWorkflow.TargetWorkflow == null)
{
ValidationError error = new ValidationError(SR.GetString(SR.Error_TypePropertyInvalid, "TargetWorkflow"), ErrorNumbers.Error_PropertyNotSet);
error.PropertyName = "TargetWorkflow";
validationErrors.Add(error);
}
else
{
ITypeProvider typeProvider = (ITypeProvider)manager.GetService(typeof(ITypeProvider));
if (typeProvider == null)
throw new InvalidOperationException(SR.GetString(SR.General_MissingService, typeof(ITypeProvider).FullName));
Type targetWorkflowType = invokeWorkflow.TargetWorkflow;
if (targetWorkflowType.Assembly == null && typeProvider.LocalAssembly != null)
{
Type workflowType = typeProvider.LocalAssembly.GetType(targetWorkflowType.FullName);
if (workflowType != null)
targetWorkflowType = workflowType;
}
if (!TypeProvider.IsAssignable(typeof(Activity), targetWorkflowType))
{
ValidationError error = new ValidationError(SR.GetString(SR.Error_TypeIsNotRootActivity, "TargetWorkflow"), ErrorNumbers.Error_TypeIsNotRootActivity);
error.PropertyName = "TargetWorkflow";
validationErrors.Add(error);
}
else
{
Activity rootActivity = null;
try
{
rootActivity = Activator.CreateInstance(targetWorkflowType) as Activity;
}
catch (Exception)
{
//
}
if (rootActivity == null)
{
ValidationError error = new ValidationError(SR.GetString(SR.Error_GetCalleeWorkflow, invokeWorkflow.TargetWorkflow), ErrorNumbers.Error_GetCalleeWorkflow);
error.PropertyName = "TargetWorkflow";
validationErrors.Add(error);
}
else
{
// Exec can't have activate receive.
Walker walker = new Walker();
walker.FoundActivity += delegate(Walker w, WalkerEventArgs args)
{
if ((args.CurrentActivity is WebServiceInputActivity && ((WebServiceInputActivity)args.CurrentActivity).IsActivating))
{
ValidationError validationError = new ValidationError(SR.GetString(SR.Error_ExecWithActivationReceive), ErrorNumbers.Error_ExecWithActivationReceive);
validationError.PropertyName = "Name";
validationErrors.Add(validationError);
args.Action = WalkerAction.Abort;
}
};
walker.Walk((Activity)rootActivity);
bool inAtomicScope = false;
Activity parentScope = invokeWorkflow.Parent;
while (parentScope != null)
{
if (parentScope is CompensatableTransactionScopeActivity || parentScope is TransactionScopeActivity)
{
inAtomicScope = true;
break;
}
parentScope = parentScope.Parent;
}
// Validate that if the workflow is transactional or being exec'd then it is not enclosed in an atomic scope.
if (inAtomicScope)
{
ValidationError validationError = new ValidationError(SR.GetString(SR.Error_ExecInAtomicScope), ErrorNumbers.Error_ExecInAtomicScope);
validationErrors.Add(validationError);
}
foreach (WorkflowParameterBinding paramBinding in invokeWorkflow.ParameterBindings)
{
PropertyInfo propertyInfo = null;
propertyInfo = targetWorkflowType.GetProperty(paramBinding.ParameterName);
if (propertyInfo == null)
{
ValidationError validationError = new ValidationError(SR.GetString(SR.Error_ParameterNotFound, paramBinding.ParameterName), ErrorNumbers.Error_ParameterNotFound);
if (InvokeWorkflowActivity.ReservedParameterNames.Contains(paramBinding.ParameterName))
validationError.PropertyName = ParameterInfoBasedPropertyDescriptor.GetParameterPropertyName(invokeWorkflow.GetType(), paramBinding.ParameterName);
validationErrors.Add(validationError);
continue;
}
Type parameterType = propertyInfo.PropertyType;
if (paramBinding.GetBinding(WorkflowParameterBinding.ValueProperty) != null)
{
ValidationErrorCollection memberErrors = ValidationHelpers.ValidateProperty(manager, invokeWorkflow, paramBinding.GetBinding(WorkflowParameterBinding.ValueProperty), new PropertyValidationContext(paramBinding, null, paramBinding.ParameterName), new BindValidationContext(parameterType, AccessTypes.Read));
if (memberErrors.Count != 0)
{
validationErrors.AddRange(memberErrors);
continue;
}
}
}
}
}
}
return validationErrors;
}
}
#endregion
}
|