Monday, April 21, 2008

Custom Activity in ASP.Net Workflow

1) Open .Net IDE, create a new project. Select “Sequential Workflow Library” under Workflow project template in “New Project” dialog box.





2) Rename workflow class to your desired class name. In my case I renamed it to “MyWorkflow” from “Workflow1”.



3) Create a folder named “Actvity” where you can place your custom activities. Click the “Activity” menu that appears when you try adding new item in Actvity folder in solution explorer.




4) Select “Activity” class template from “Add New Item” dialog box. Rename the activity class to your desired one.




5) In activity class override the “Execute” method. Write your own code in that Execute method.

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
Console.WriteLine("This is my custom Actvity");

return base.Execute(executionContext);
}


6) Build/Save the application to see your custom activity in the Workflow designer toolbox.




7) Drag-n-Drop your custom activity from toolbox to your workflow designer area.
Rename the activity name if you want.




8) This Drag-n-Drop action will insert the code automatically in “MyWorkflow.Designer.cs” file. Here you can modify the code if you want.

#region Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

[System.Diagnostics.DebuggerNonUserCode]
private void InitializeComponent()
{
this.CanModifyActivities = true;
this.MyActivity = new MyWorkFlow.Activity.MyActivity();
//
// MyActivity
//
this.MyActivity.Name = "MyActivity";
//
// MyWorkflow
//
this.Activities.Add(this.MyActivity);
this.Name = "MyWorkflow";
this.CanModifyActivities = false;

}

#endregion


9) Now comes the part to consume your custom workflow and custom activity. Add a new console project in your solution explorer.



10) Add references for following two dlls in your Console Application reference section:
a. System.Workflow.Activities
b. System.Workflow.ComponentModel
c. System.Workflow.Runtime
Add project reference of your Custom Workflow project, so that you need not to add your Workflow project dll again and again after each modification.


11) Add following “using” directive in your console application class:
a) using System.Threading;
b) using System.Workflow.Runtime;
c) using System.Workflow.Runtime.Hosting;

12) Add following code in your “Main” method:


using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
AutoResetEvent waitHandle = new AutoResetEvent(false);

workflowRuntime.WorkflowCompleted +=
delegate(object sender, WorkflowCompletedEventArgs e)
{
waitHandle.Set();
};

workflowRuntime.WorkflowTerminated +=
delegate(object sender, WorkflowTerminatedEventArgs e)
{
Console.WriteLine(e.Exception.Message);
waitHandle.Set();
};

WorkflowInstance instance =
workflowRuntime.CreateWorkflow(typeof(MyWorkFlow.MyWorkflow));
instance.Start();

waitHandle.WaitOne();
}

The above code adds your custom workflow instance in your console application.

13) Before testing the whole application, you have set your Console application project as “StartUp Project”. You can set your project as StartUp Project by clicking the desired project in solution explorer and click the “Set as StartUp Project” menu.

Also write following code as a last line in your Console application’s main method:

Console.ReadLine();

The above code keep the console screen open, for getting the user response.

14) Now run the application, you will get following console screen:


The above message is written in your Custom activity “Execute” method.

Google