Your First Simple Basic Asp.net Workflow
When I tried to jump into the asp.net workflow platform, I found out there are really very few simple/basic examples available in the internet. Most articles deal with huge business concepts,utilizing different workflowservices, that can be really confusing for someone new to workflow development.Here I have tried a simple hello world Type of Asp.net Workflow..this is not bad for a startup.

The concept is really simple.You have a webpage default.aspx , with a textbox and a button. You enter an amount in the textbox and if the amount is more than 100, you are redirected to the credit.aspx and if less, to the debit.aspx. To start, Place the code from my previous post, into the application_start and application_end section of  your Global.asax page.

Add a sequential workflowLibrary  project to your solution.(my one is named workflowLibrary2).You see there is a sequential workflow  workflow1 added to the project. Drop a ifElseActivity from the Toolbox.


Now in the left ifElseactivity, set the condition property as codecondition and define the condition as ValidateAmount.Double clicking it will take you the codebehind for the workflow1.cs.Place 2 codeactivity objects on both the branches. I will come back to this later.

Now on the codebehind section of the workflow1.cs, rightclick-->insertsnippet-->other-->workflow-->WorkflowDependencyProperty. Like this you make two properties like below.

 public static DependencyProperty AmountProperty = DependencyProperty.Register("Amount", typeof(Int32), typeof(Workflow1));

        [DescriptionAttribute("Amount")]
        [CategoryAttribute("Amount Category")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public Int32 Amount
        {
            get
            {
                return ((Int32)(base.GetValue(Workflow1.AmountProperty)));
            }
            set
            {
                base.SetValue(Workflow1.AmountProperty, value);
            }
        }
        public static DependencyProperty ResultProperty = DependencyProperty.Register("Result", typeof(string), typeof(Workflow1));

        [DescriptionAttribute("Result")]
        [CategoryAttribute("Result Category")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string Result
        {
            get
            {
                return ((string)(base.GetValue(Workflow1.ResultProperty)));
            }
            set
            {
                base.SetValue(Workflow1.ResultProperty, value);
            }
        }
The first property is integer and the second one string.

Now come back to the ifElseActivity Condition. We are just going to check if the amount entered is more than 100.

 private void ValidateAmount(object sender, ConditionalEventArgs e)
        {
            if (this.Amount > 100)
                e.Result = true;
        }
The conditionaleventArg e is evaluated here. If the condition is satisfied assign e.Result as true, so the activity associated with it will be executed. No condition is specified for the other branch, so it will just act as an else clause. Now define ExecuteCode property of the CodeActivity objects by  selecting  the ExecuteCode property, writing down the name of the method for it and doubleClicking it. For CodeActivity1 I am writing RedirectCredit and for CodeActivity2  RedirectDebit.


        private void RedirectCredit(object sender, EventArgs e)
        {
            this.Result = "Credit.aspx";
        }

        private void RedirectDebit(object sender, EventArgs e)
        {
            this.Result = "Debit.aspx";
        }

The two dependency properties that  I defined above are going to be used as parameters of the workflow. The easiest and simple way to pass parameters between the WorkFlow host application and the Workflow instance is to use Dictionary Objects collection. I am going to show how in the next section.

In the codebehind of the Default.aspx write code for the Button click event.

 protected void Button1_Click(object sender, EventArgs e)
    {
        InitiateWorkFlow();
    }
Define the InitiateWorkFlow method.

private void InitiateWorkFlow()
    {
        Dictionary<string, object> parameters = new Dictionary<string, object>();
        parameters.Add("Amount",Convert.ToInt32(TextBox1.Text));
        parameters.Add("Result", "");
        WorkflowRuntime wfRuntime = Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;
        wfRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(wfRuntime_WorkflowCompleted);
        WorkflowInstance wi = wfRuntime.CreateWorkflow(typeof(WorkflowLibrary2.Workflow1), parameters);
        wi.Start();
        ManualWorkflowSchedulerService ss = wfRuntime.GetService<ManualWorkflowSchedulerService>();
        ss.RunWorkflow(wi.InstanceId);
        //HttpContext.Current.Session["WorkflowID"] = wi.InstanceId;
    }
Lets describe it. First we declare a Dictionary collection object  'parameters'. This is the parameter object that the host is going to use to pass on to the workflow. I add two parameters amount and result, to the collection.Then I declare a WorkflowRuntime Object and load it with the runtime object from the application object, that was created in the application_Start  event. I am also adding an eventhander for the WorkflowCompleted Event.Then the WorkFlowInstance is created and look how the parameters object is passed on to it in the CreateWorkflow method. The 2 params created here will be mapped on to the Dependency properties in the workFlow. Then the workflow instance is started.

Next, remember the   ManualWorkflowSchedulerService that we added to the WF Runtim, in the Application_start event. We retrieve that   ManualWorkflowSchedulerService by using the GetSerive method of the wfRuntime object. Then we execute the RunWorkFlow method of the   ManualWorkflowSchedulerService. We are doing this because this will use the delayactivity built in this newly developed   ManualWorkflowSchedulerService, so that once the host thread starts executing, it will stop for the workFlow thread to finish executing and resume itself again. This is synchronized execution of both the Host request and workflow runtime on the same thread.

Next we describe the WorkFlowCompleted Event.

 static void wfRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
    {
        HttpContext.Current.Response.Redirect((string)e.OutputParameters["Result"]);  
    }
In this method, observe that the eventArgument is called WorkflowCompletedEventArgs. This argument e has a collection called Outputparametes which holds the Workflow paramater collection. So we can retrieve the value of the WorkFlow Parameter  'Result' and use it for our redirection of page.
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Comments

No comments posted yet.

Leave Your Comment

Title*
Name*
Email (never displayed)
 (will show your gravatar)
Url
Comment*

 

Preview Your Comment.