Schedule children DynamicActivity loaded from external XAML with parameters

心不动则不痛 提交于 2020-01-04 09:37:03

问题


Scenario: I'm implementing a parent activity which executes other activity from external source(database), following this Ron Jacobs post.

This approach works but have a few problems in my case, because WorkflowInvoker don't get the parent extensions:

  • Tracking is disabled for childrens
  • My custom data sharing extension don't work
  • Extensions can change depending on the host, so I can't simply add new ones again.

Potential solution: Instead of Invoke the children XAML, I'm scheduling it(I believe it will solve my problems, right?).

CacheMetadata: load the DynamicActivity from the external source and call metadata.AddChild(_childActivity);.

Then execute:

    protected override void Execute(NativeActivityContext context)
    {
        context.ScheduleActivity(_childActivity, OnActivityComplete);
    }

And it worked! Next step would be pass In, Out and InOut arguments to children.

Problem: How to Schedule children DynamicActivity loaded from external XAML with InArgument, OutArgument and InOutArgument values

Something I'm doing but is working just for OutArguments. In CacheMetadata I called my method _childActivity.BindArguments

    public static void BindArguments(this DynamicActivity activity, IDictionary<string, Argument> argumentsToBind)
    {
        if (argumentsToBind == null)
            return;

        Type genericPropType, valueType, sourceArgumentType, vbReferenceType;
        Argument sourceArgument;
        foreach (var destinyArgument in activity.Properties)
        {
            if (!argumentsToBind.TryGetValue(destinyArgument.Name, out sourceArgument))
                continue;

            genericPropType = destinyArgument.Type.GetGenericTypeDefinition();
            if (genericPropType == typeof(InArgument<>))
            {
                destinyArgument.Value = new InArgument<string>("It worked! But I need the value from context which is unavaliable since I'm inside CacheMetadata");
            }
            else
            {
                valueType = destinyArgument.Type.GetGenericArguments()[0];
                sourceArgumentType = genericPropType.MakeGenericType(valueType);

                if (sourceArgument != null)
                {
                    vbReferenceType = typeof(VisualBasicReference<>).MakeGenericType(valueType);

                    object vbReference = Activator.CreateInstance(vbReferenceType,
                        GetExpressionText(sourceArgument, sourceArgumentType));

                    object referenceArgument = Activator.CreateInstance(sourceArgumentType, vbReference);

                    destinyArgument.Value = referenceArgument;
                }
            }
        }
    }

So, I need to pass InArguments and InOutArguments too but I need the value from context which is unavaliable since I'm inside CacheMetadata.

I tried to set the DynamicActivityProperty.Value in the Execute method. But it doesn't worked too.

After, I founded this page that may help you to understand my scenario.


回答1:


Instead of doing all of this have you considered passing messages to the child workflow? Workflows hosting other workflows is not an area that is well supported (as you have discovered). Instead Workflows sending messages to other workflows seems to be a better approach. Of course, you can't share tracking and extensions. I did come up with a way to support tracking in the child workflow but it uses private reflection.



来源:https://stackoverflow.com/questions/9501330/schedule-children-dynamicactivity-loaded-from-external-xaml-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!