Introduction: The InvokeMethod activity is another way to implement a code which is outside of the standard built-in activities. You can use this activity to invoke a method of a class. The class does not need to be part of the workflow or use any of the workflow base classes. The InvokeMethod calls a public method of a specified object or type.

InvokeMethod Properties:

InvokeMethod Class:

Namespace: System.Activities.Statements.
Assembly: System.Activities (in System.Activities.dll).

Step 1: Open Visual Studio 2010.

i1.gif

Define Arguments:

Step 2: Go to the Workflow.xaml file and double-click on the Arguments option.

I2.gif

Step 3: Drag Activity from Toolbox.

i3.gif

Step 4: Select Sequence and While activity and create variables.

i5.gif

Step 5: Drag Sequence, Assign, InvokeMethod activity inside the body of While activity.

i7.gif

Step 6: Go to properties of InvokeMethod and define MethodName, TargetObject.

pi.gif

Step 7: Define Parameters for InvokeMethod.

para.gif

Step 8: Go to the Programe.cs file and write the below code.


using System;
using System.Linq;
using System.Activities;
using System.Activities.Statements;
using System.Activities.Expressions;
using System.Collections.Generic;
using System.ServiceModel.Activities;
using System.Runtime.Serialization;
using System.Collections;
namespace WorkflowConsoleApplication23
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter string: ");
string reversedString = string.Empty;
IDictionary<string, object> results = WorkflowInvoker.Invoke(new ReverseStringWorkflow(),
new Dictionary<string, object>{ {"StringToReverse", stringToReverse } });
Console.Write("Reversed string: ");
Console.WriteLine(results["ReversedString"]);
Console.ReadKey();
}
}
}

Creating Workflow using code:

Step 9: Write the code for creation of Workflow.

class.gif

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Statements;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace
WorkflowConsoleApplication23
{
class Class1
{
public class ReverseStringWorkflow : Activit
{
public InArgument<string> StringToReverse { get; set; }
public OutArgument<string> ReversedString { get; set; }
protected override Func<Activity> Implementation
{
get
{
return () =>
{
Sequence sequence = new Sequence
{
Variables = new Variable<StringBuilder>("ReverseStringBuilder", new StringBuilder())},
Activities = new While
{
Variables = new Variable<int>("Counter")
{
Default = new VisualBasicValue<int>("StringToReverse.Length")
}
},
Condition = new VisualBasicValue<bool>("Counter > 0"),
Body = new Sequence
{
Activities =
{
new Assign
{
To = new OutArgument<int>(new VisualBasicReference<int>("Counter")),
Value = new InArgument<int>(new VisualBasicValue<int>("Counter - 1"))
},
new InvokeMethod
{
MethodName = "Append",
TargetObject = new InArgument<StringBuilder>(new VisualBasicValue<StringBuilder>("ReverseStringBuilder")),
Parameters =
{
new InArgument<char>(new VisualBasicValue<char>("StringToReverse.Chars(Counter)"))
}
}
}
}
},
new Assign|
{
To = new OutArgument<string>(new VisualBasicReference<string>("ReversedString")),
Value = new InArgument<string>(new VisualBasicValue<string>("ReverseStringBuilder.ToString()"))
}
}
};
return sequence;
};
}
set
{
base.Implementation = value;
}
}
}

Step 10: Press F5 to run the application.

output.gif