How to create a custom activity in WF 4 deriving from CodeActivity?

Posted: January 11, 2011 in WF4.0
Tags: , , , , , , ,

This article gives a basic idea of how to create a custom activity in WF 4 deriving from CodeActivity.Steps below demonstrates how to do this.

• Open a New Project in vs2010,Choose WCF WorkFlow Service Application

• You can see Service1.xamlx added.Rename Service1.xamlx to Operation.xamlx

• Now Add a class to this project & name it as OperationActivity.cs

• Add namespace using System.Activities; to class

• Derive Class from CodeActivity

public class OperationActivity:CodeActivity
• Add In & Out arguments

[RequiredArgument]
public InArgument From { get; set; }
public OutArgument CallCount { get; set; }

Write the below code & compile. What this activity does is, takes an InArgument of type string. If it is not empty, increase the counter set and assign it to OutArgument CallCount.

namespace Anupama_s_WF_Sample
{
public class OperationActivity:CodeActivity
{

[RequiredArgument]
public InArgument From { get; set; }
public OutArgument CallCount { get; set; }
protected override void Execute(CodeActivityContext context)
{

try
{

string text = context.GetValue(this.From);
int count=0;
if (!string.IsNullOrEmpty(text))
{
count++;
context.SetValue(this.CallCount,count);

}

}
catch (Exception ex)
{
throw;
}
}

}
}

Now Open xamlx designer, you see two Activities added by default

1.ReceiveRequest

Receives the request from user

2.SendResponse

Sends result back to user

Open Tool Box as you see in the snapshot, you can see the compiled custom activity OperationActivity which you created now. Drag & drop as shown in snapshot.

It shows a validation symbol highlighted in red .It means value for the arguments are not supplied

• For this you have to add a variable

• Name variable as “From” .Set the variable type as string

• Click on receive request activity

Against Content,You can see View parameter.Click on it.It will open a dialog box.Click on Parameters radio button.Add new parameter FromInput & assign it to the previously created variable “From”.Click ok

• Select Operation Activity,Right Click to see properties.


• Against From in properties window,select the From variable declared.

• What we have done is,ReceiveRequest activity takes the input from user,assigns it to From variable & Operation activity utilizes the value from common variable set.

• We need one more variable to pass the output from custom activity to SendResponse activity

• Create variable Result of type int

• Assign Operation activity CallCount to Result,Add this Result as the input parameter to SendResponse activity.

Look at the designer now, you can see that all validation errors are disappeared as we satisfied the input & output requirements by assigning to appropriate variables.

Hope you got the basic idea of how to create a custom activity deriving from CodeActivity.

Advertisements
Comments
  1. Jane says:

    Thanks for the post. Indeed helpful.

  2. Anupama says:

    You are Welcome Jane!

  3. Juan Pablo says:

    Great post, greetings

    • Hi Ana, this is indeed very insightful. I’ll note that for .NET 4.5 I just had to change these types and it purrs like a kitten:

      from

      public InArgument From { get; set; }
      public OutArgument CallCount { get; set; }

      to
      public InArgument From { get; set; }
      public OutArgument CallCount { get; set; }

      Mike

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s