views:

114

answers:

1

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

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 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?

+1  A: 

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.");
    }
}
Kevin Driedger