locked
Using InvokeMethod Activity of WF 4.0 RRS feed

  • Question

  • Hi All

       I am working with WF 4.0 where I wants to call a method from external class in WF using InvokeMethod Activity, but I am facing lots of problem while doing it.
      My project setings are as below:
    1. Class and WF are in same namespace.
    2. All methods in class are instance methods and public.
    3. I have set TargetObject property of the InvokeMethod Activity to the object of the class.
    4. The method name property of the activity is set to the name of the method as string means in "".

    but now my method has some input parameters (string array), now I am not getting hoe to set the "Parameters" property of the InvokeMethod activity, if I try to set it it give me designer error (Red colored Cross). 

    So can anybody help me on this.

    Regards

     
      
    Tuesday, July 7, 2009 6:49 AM

Answers

  • You don't need to specify the TargetType of the activity to invoke an instance method. TargetType is intended to invoke static methods and TargetObject for instance methods.

    If you want to invoke the method MyMethod as defined below:

    public class MyClass
    {
        public void MyMethod(string[] parameters)
        {
            foreach (string s in parameters)
            {
                Console.WriteLine(s);
            }
        }
    }

    you can configure your InvokeMethod as follows:
    - TargetObject: an instance of MyClass
    - MethodName: MyMethod
    - Parameters: create an InArgument of type string[] and set the value

    For example, if you want to invoke MyMethod in a new instance of MyClass and pass the array "a", "b", "c", this is  how you configure InvokeMethod

     - TargetObject: new MyClass
     - MethodName: MyMethod
     - Parameters:
            - Add a new argument.
                  - Direction: In
                  - ArgumentType: string[] (first select Array of [T] in the dropdown and then specify that T is a string)
                  - Expression: {"a", "b", "c" } (please note that expression could be also a variable containing the values)

    -Leon
    Saturday, July 11, 2009 12:40 AM

All replies

  • Hi Mahesh,

    First i guess when ever  you drag and drop InvokeMethod Activity, you have to set Target Type to Class\Type in which method has been return.  You can also do this with Target object though i didn't try.

    Then for passign input parameters, create varaibles of specific types and bind those variables into Parameter collections. You can set value thorugh xaml into varaibles or in expression editor.

    I haven't try for String[] but I got success in filling HashTable thorugh xmal into a variable type<hashTable> here is the code for same. it may help you out. However still could not get success to fill it through designer's Expression window!!!


    For List of string type:-

    <scg:List x:TypeArguments="x:String" Capacity="4">

            <x:String>Hello1</x:String>

            <x:String>Hello2</x:String>

          </scg:List>


    For Array of string Type:- 

    <x:Array Type="x:String">

            <x:String>Hi</x:String>

            <x:String>Hello</x:String>

            <x:String>H2</x:String>

          </x:Array>

     

     

    For HashTable type:-

     

    <p:Variable x:TypeArguments="sc:Hashtable" Name="InputParams1">

            <p:Variable.Default>

              <p:Literal x:TypeArguments="sc:Hashtable">

                <sc:Hashtable>

                  <x:Int32>1<x:Key><x:String>key1</x:String></x:Key></x:Int32>

                </sc:Hashtable>

              </p:Literal>

            </p:Variable.Default>

          </p:Variable>


    Thanks.

    Darshak Vaishnav(Eclipsys India)

     

    Wednesday, July 8, 2009 1:17 PM
  • Thanks Darshak,

       Thanks, what I did is I specified the Target Type of the activity,   as the name of the class from which I wants to call method, the Method Name property is set to the name of the method. Now since my method waS initially taking input parameter as string[] I was facing problems, actually I used these string array for my input parameters to a method, so I put these input parameter in one class and then created a variable using  this class. Then to set parameters property of the activity,  input parameter is declared with "In" as direction the "Argument Type" is set to the Name of the class of which object I am passing to the method and then in "Expression" I have put the variable  which I declared of this class type.This worked for me.

    Regards

    Mahesh Sabnis
    Thursday, July 9, 2009 9:06 AM
  • You don't need to specify the TargetType of the activity to invoke an instance method. TargetType is intended to invoke static methods and TargetObject for instance methods.

    If you want to invoke the method MyMethod as defined below:

    public class MyClass
    {
        public void MyMethod(string[] parameters)
        {
            foreach (string s in parameters)
            {
                Console.WriteLine(s);
            }
        }
    }

    you can configure your InvokeMethod as follows:
    - TargetObject: an instance of MyClass
    - MethodName: MyMethod
    - Parameters: create an InArgument of type string[] and set the value

    For example, if you want to invoke MyMethod in a new instance of MyClass and pass the array "a", "b", "c", this is  how you configure InvokeMethod

     - TargetObject: new MyClass
     - MethodName: MyMethod
     - Parameters:
            - Add a new argument.
                  - Direction: In
                  - ArgumentType: string[] (first select Array of [T] in the dropdown and then specify that T is a string)
                  - Expression: {"a", "b", "c" } (please note that expression could be also a variable containing the values)

    -Leon
    Saturday, July 11, 2009 12:40 AM
  • We have a great sample on using the InvokeMethod activity.  Just check out the InvokeMethod Sample on MSDN


    Sr. Program Manager, Windows Azure Platform Team(WF) http://blogs.msdn.com/rjacobs http://www.twitter.com/ronljacobs
    Tuesday, December 20, 2011 5:25 PM
  • The InvokeMethodUsage Designer Sample does not work in Visual Studio due to a XAML Parsing error.
    Thursday, May 24, 2012 6:49 PM