Pages

Tuesday, January 9, 2018

Invoke a code activity (Native Activity) and retrieve the results using code – Workflow Foundation 4.5

Introduction

We can create code activity as a part of workflow solutions using workflow foundation 4.5. In this post I will show how we can create a native code activity and access the results in asp.net controller. Native code activity

 

   //The string mentioned in NativeActivity is the resut type.  

     public sealed class MySampleNativeCode : NativeActivity     {

         // Define an activity input argument of type string

         public InArgument Name { get; set; }



         protected override void Execute(NativeActivityContext context)

         {

             var name = context.GetValue(Name);

             //Here we set the string value to the Result property in NativeCode

             context.SetValue(base.Result, "Hello from Native Code Activity " + name);



         }

     }
Invoke the code activity The code activity is invoked in asp.net web api controller.
 
 // GET: api/CustomerRegistration

         public string Get()

         {

             //Create a instance of the native code

             Activity wf = new MySampleNativeCode();



             //Define the input argumentes

             Dictionary arguments = new Dictionary();

             arguments.Add("Name", "Suhail");



             //Invoke the workflow

             IDictionary outputs = WorkflowInvoker.Invoke(wf, arguments);



             //Get the output value and assign to return string

             var returnString = outputs["Result"].ToString();



             return returnString;

         }

Conclusion

You can see the value in “returnString” which is returned from Native Code Activity.



No comments:

Post a Comment