Today, I am going to share one more finding that I explored in last few days. It took my ample amount of time and at last found some solution.
So here, I was working on Workflow foundation4 and was passing arguments to it through designer. A lot of major changes made in WF4, which requires to learn lots of things. here I would be concentrating on passing arguments and few more things.
So I have created a workflow (Also called as activity) and created few In Arguments and Out Arguments. You can see about this in details here.
So I have created a sample workflow that takes few In Argument and Out arguments some of Int type and some string type. So while invoking the workflow I need to pass these parameter.
So I have created a workflow HelloFriend.xaml and which takes name as string and age as int of the as In parameter and return a WelcomeMessage of string type.
Now let’s see the code which invoke it.
IDictionary inputs = new Dictionary() { {"name","Brij"}, {"age",28} }; IDictionary output = WorkflowInvoker.Invoke(new HelloFriend(), inputs); string fullName = output["WelcomeMessage"] as string;
As you can see workflow takes the inputs in Dictionary format and returns the output also in Dictionary format. As you can see I am passing name and age in it.
But Now let’s try to pass some arguments of custom type.
I have created two Classes one RequestType and Other ResponseType in a separate library. RequestType is as
public class RequestType { public string FirstNasme { get; set; } public string LastName { get; set; } }
And ResponseType is
public class ResponseType { public string Fullname { get; set; } }
Now I have added the reference to my workflow project. As worklow4 allows us to pass parameters of any custom type, I added a two parameters on In request and another response of RequestType and ResponseType respectively. Now I tried to invoke this workflow as
RequestType request = new RequestType() { FirstNasme = "Brij", LastName = "Bhushan" }; IDictionary inputs = new Dictionary() { {"request",request}, }; IDictionary output = WorkflowInvoker.Invoke(new HelloFriendCustomType(), inputs); string fullName = ((ResponseType)(output["response"])).Fullname;
But I got this runtime error on the line where workflow is invoked:
Literal’: Literal only supports value types and the immutable type System.String.
I tried to search the solution internet but could not find easily. Later I found the solution and I got to know if we want to pass some custom type then I need to pass it in as a lambda value. Then I converted the code as
RequestType request = new RequestType() { FirstNasme = "Brij", LastName = "Bhushan" }; IDictionary inputs = new Dictionary() { {"request",new InArgument(new LambdaValue((env)=> request))}, }; IDictionary output = WorkflowInvoker.Invoke(new HelloFriendCustomType(), inputs); string fullName = ((ResponseType)(output["response"])).Fullname;
It started running like a charm.
It means we cannot pass a custom type In arguments by just assigning the passing the object.We need to assign it as lambda format.
Sometimes we load the workflows dynamically. And these can be loaded as
Activity act = ActivityXamlServices.Load("HelloFriendCustomType.xaml");
Now if you want to pass some parameters in dynamically loaded workflows, then How will you do this? This should be done in just simple way.
Activity act = ActivityXamlServices.Load("HelloFriendCustomType.xaml"); RequestType request = new RequestType() { FirstNasme = "Brij", LastName = "Bhushan" }; IDictionary inputs = new Dictionary() { {"request",request}, }; IDictionary output = WorkflowInvoker.Invoke(act, inputs); string fullName = ((ResponseType)(output["response"])).Fullname;
Hope this will save lot of time to workflow4 developers.
Thanks,
Brij
Hello
please, is it possible to have the code??
Right now, I don’t have the code. I can do it but I would prefer if you post the code where you find the issues.