Custom WF4 Activity with OutArgument and Assign Activity
I am trying to write a custom activity by composing standard Activities, one of which is an Assign activity which is responsible for assigning a string value to an OutArgument, called 'TextOut', which I have def开发者_JAVA技巧ined on my custom Activity. The idea is that the workflow author that uses this custom Activity defines a variable in the Workflow and maps this to the TextOut OutArgument of my custom Activity. I'd like to achieve this with an iterative approach, since I have a requirement to dynamically create pick branches at runtime. I have left out this code to simplify my question.
The code for the Activity is shown below. This is probably not how it should be done, since it doesn't work :) The workflow which uses this Activity throws a validation error: "Value for a required activity argument 'To' was not supplied".
I'd like to get some advice on how to get my OutArgument working with an Assign child activity (so without calling .Set on my OutArgument).
public sealed class OutArgActivity : Activity
{
public OutArgument<string> TextOut { get; set; }
public OutArgActivity()
{
Assign assign = new Assign {
To = this.TextOut,
Value = new InArgument<string>(
env => "this is my custom return value")
};
Sequence sequence = new Sequence();
sequence.Activities.Add(assign);
this.Implementation = () => sequence;
}
}
Try using a ArgumentReference in your Assign activity like this:
public sealed class OutArgActivity : Activity
{
public OutArgument<string> TextOut { get; set; }
public OutArgActivity()
{
Assign<string> assign = new Assign<string>
{
To = new ArgumentReference<string>("TextOut"),
Value = new InArgument<string>(
env => "this is my custom return value")
};
Sequence sequence = new Sequence();
sequence.Activities.Add(assign);
this.Implementation = () => sequence;
}
}
If you don't want to use magic strings you can do it this way.
public sealed class OutArgActivity : Activity
{
public OutArgument<string> TextOut { get; set; }
public OutArgActivity()
{
Assign<string> assign = new Assign<string>
{
To = new OutArgument<string>(ctx => TextOut.Get(ctx)),
Value = new InArgument<string>(
env => "this is my custom return value")
};
Sequence sequence = new Sequence();
sequence.Activities.Add(assign);
this.Implementation = () => sequence;
}
}
This seems counter intuitive, but since the argument to the constructor of OutArgument<>
is an Expression
it is possible to convert it to a location reference. And that is exactly what happens.
精彩评论