Search code examples
workflowworkflow-foundation-4workflow-foundationworkflow-activity

WF 4 different IDs on the same activities


Due to a strange behavior in my application, i am forced to reload the designer before calling WorkflowInvoker.Invoke on it.

wd.Flush();
SaveXamlFile(currentXamlPath, wd.Text);

I just flush the content, and write the wd.Text to a file.

//cleanup the previous designer
if (wd != null)
{
    wd.ModelChanged -= new EventHandler(Designer_ModelChanged);
}

//designer
wd = new WorkflowDesigner();
designerArea.Child = wd.View;
this.DebuggerService = this.wd.DebugManagerView;

//property grid
propertiesArea.Child = wd.PropertyInspectorView;

//event handler
wd.ModelChanged += new EventHandler(Designer_ModelChanged);

//error service
wd.Context.Services.Publish<IValidationErrorService>(errorService);


wd.Context.Items.Subscribe<Selection>(OnItemSelected);

I then recreate a new instance of the WorkflowDesigner and load the previously saved file.

wd.Load(currentXamlPath);

I call WorkflowInvoker.Invoke and inside my custom activity which derives from CodeActivity i am taking it's name:enter image description here OK, fine until now, i have a 1.2 Id there.

I want to update some of the fields of this Activity via its ModelItem in order to display them in the GUI right away.

IEnumerable<ModelItem> activityCollection = currentWorkflow.Find(currentWorkflow.Root, typeof(Activity));

But here comes the issue:enter image description here

I can't find that my Activity id there. Is now transformed from 1.2 to 2. Why is this happening?

I've tried to send a this reference from my Activity Execute method and searched it by ref but all i get is nulls.

ModelItem temp = activityCollection.FirstOrDefault((m) => (m.GetCurrentValue() == a));

I am sure i am missing something here, but i can't figure out what is it.


Solution

  • I found a workaround on this :

    On my custom activities i am adding a Guid property and I override CacheMetadata:

    public Guid unique { get; set; }
    
    protected override void CacheMetadata(CodeActivityMetadata metadata)
    {
        if (unique.ToString() == "00000000-0000-0000-0000-000000000000")
            unique = Guid.NewGuid();
    }
    

    When i drag the activity on the designer, the unique id is generated. I make sure that this portion of code is called only once. Why is that?

    Because after a call like this,

    IEnumerable<ModelItem> activityCollection = currentWorkflow.Find(currentWorkflow.Root, typeof(Activity));
    

    each model in the activity collection contains that property ( unique of type Guid ) with the value of the first assignment made in CacheMetadata. I can't explain this behavior, i've just taken it into consideration.

    Who calls again that CacheMetadata ? something like this :

    Activity root = ActivityXamlServices.Load(currentXamlPath);
    WorkflowInspectionServices.CacheMetadata(root);
    

    And so, the Guid is changed and its utility is gone.

    This way, i am able to get the ModelItem for my custom activity and update some of its properties which are immediately displayed in the GUI.