开发者

How to invoke a method from a workflow in WF 4?

I would like to invoke a simple method (no arguments, returns void) from within a workflow. Suppose I have the following class:

public c开发者_如何学编程lass TestClass
{
    public void StartWorkflow()
    {
        var workflow = new Sequence
        {
            Activities =
            {
                new WriteLine { Text = "Before calling method." },
                // Here I would like to call the method ReusableMethod().
                new WriteLine { Text = "After calling method." }
            }
        }
        new WorkflowApplication(workflow).Run();
    }

    public void ReusableMethod()
    {
        Console.WriteLine("Inside method.");
    }
}

How do I call ReusableMethod from within my workflow? I was looking at InvokeAction but that doesn't seem to be what I want. I could also write a custom activity that calls this method, but I'm specifically interested in this scenario. Is this possible?


How about InvokeMethod ?

public class TestClass
{
    public void StartWorkflow()
    {
        var workflow = new Sequence
                        {
                            Activities =
                                {
                                    new WriteLine {Text = "Before calling method."},
                                    // Here I would like to call the method ReusableMethod().
                                    new InvokeMethod {MethodName="ReusableMethod", TargetType = typeof(TestClass)},
                                    new WriteLine {Text = "After calling method."}
                                }
                        };
        var wf = new WorkflowApplication(workflow);
        wf.Run();
        var are = new AutoResetEvent(false);
        wf.Completed = new Action<WorkflowApplicationCompletedEventArgs>(arg => are.Set());
        are.WaitOne(5000);
    }

    public static void ReusableMethod()
    {
        Console.WriteLine("Inside method.");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜