Windows Workflow Foundation enables the developers to create workflow enabled applications. A typical workflow application can be a helpdesk application where the user submits a ticket and his request is answered in a certain manner.

Windows Workflow Foundation enables the developers to create workflow enabled applications. A typical workflow application can be a helpdesk application where the user submits a ticket and his request is answered in a certain manner.

Here is a simple workflow that deposit and withdraw the amount from the user's account. This workflow is in no way complete and this is just to show how to run a workflow from an ASP.NET application.

First, the user is authenticated. After authentication the user's amount is deposited or withdrawn from his account.



Here is the code that runs behind the workflow.

public sealed partial class BankAccountWorkflow: SequentialWorkflowActivity
    {
        private User _user = new User();

        private bool _isCompleted;

        public bool IsCompleted
        {
            get { return _isCompleted; }
            set { _isCompleted = value; }
        }
     
        public User User
        {
            get { return _user; }
            set { _user = value; }
        }

        public BankAccountWorkflow()
        {
            InitializeComponent();
        }

        private void AuthenticateUser(object sender, EventArgs e)
        {
            UserManager.AuthenticateUser(_user);
        }

        private void DepositAmount(object sender, EventArgs e)
        {
            bool result = UserManager.DepositAmount(_user);
            if (result)
                _isCompleted = true;
            else _isCompleted = false;
        }

        private void WithdrawAmount(object sender, EventArgs e)
        {
            bool result = UserManager.WithdrawAmount(_user);
            if (result)
                _isCompleted = true;
            else _isCompleted = false;
        }
    }


As, you can see I am NOT even authenticating the user and just letting them into the system.

Now, let's see the client code which is used to execute the workflow.

 protected void Btn_Submit_Click(object sender, EventArgs e)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();

            parameters.Add("User", new User() { UserName = txtUserName.Text, Password = txtPassword.Text,
                                              TransactionType = ddlTransType.SelectedValue, Amount = Double.Parse(txtAmount.Text) });

            WorkflowRuntime wfRuntime = Global.wrRuntime;
            wfRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(TransactionCompleted);
                                   
            WorkflowInstance instance = wfRuntime.CreateWorkflow(typeof(BankAccountWorkflow),parameters);

           
            // start the workflow
            instance.Start();                   
        }

        void TransactionCompleted(object sender, WorkflowCompletedEventArgs e)
        {
            string message = ( (bool) e.OutputParameters["IsCompleted"] == true) ? "Transaction completed successfully" : "Error in the transaction";
           
        }



I hope you got some idea on how to trigger the workflow from the ASP.NET application. In the future I will provide a full article on implementing the same scenario.