18 June, 2008

Windows Workflow, DependancyProperties and Complex objects

If you want to use a complex type as a dependencyPropery in e.q. a Workflow, you might run into problems. I today wanted to use a List<T> as a dependencyPropery in Windows Workflow only to find myself in dire straits.

Like any other class that is to be serialized, the complex properties (like collections) need to be explicitly created in the constructor. All workflows are serializable to allow the WF scheduler to "route" them into the appropriate execution thread within the WFRuntime. Below is the dependency property in question.

/// <summary>
///
Gets the workflow ids started by this Activity.
/// </summary>
/// <value>
The workflow ids.</value>
[Description("The workflows started by this activity. This is set automatically.")]
public List<Guid> WorkflowIds
{
get { return (List<Guid>)GetValue(WorkflowIdsProperty); }
set { SetValue(WorkflowIdsProperty, value); }
}

// Using a DependencyProperty as the backing store for WorkflowIds. This enables animation, styling, binding, etc...
/// <summary>
///
/// </summary>
public static readonly DependencyProperty WorkflowIdsProperty =
DependencyProperty.Register("WorkflowIds", typeof(List<Guid>), typeof(ESSMoveToHearingRuntime));


I attempted this with a Workflow Activity only to realize that it was everything but helpful. The Collection was null when I was in the (overridden) Execute method in the workflow activity. So what to do? It turns out that there is a InitializeProperties method to handle this problem. Initialize your complex objects in the method and they are fully functional when you arrive in the Execute method of an Activity.


protected override void InitializeProperties()
{
base.InitializeProperties();
if(WorkflowIds == null)
WorkflowIds = new List<Guid>();
}

StackOverflow Tags:

No comments:

InRiver: Not loading your extensions?

(You really need to in the loop to appreciate the issue this post addresses). Man, I've been fighting this problem for hours before I ...