Thursday, December 23, 2010

Working in Work Flow

OMG,

Hey Flocks doing a long research in windows workflow foundation at last found some useful stuffs to update finally I won the game designed a simple workflow. Flocks dn't do anything jst follow these simple step yo knw wat I m in very urge to finish the blog so no image can be posted so excuse me for tat


Use:

The example demonstrate running WF in ASP.NET site. The WF will function as a number divider.
Input number1 and number2 WF will calculate number1/number2.

To run this sample project:
1. Run in Visual Studio 2008. After loading ASPNETSite4WF, right click Default.aspx and click
"View in Browser".
2. Run In IIS. Copy ASPNETSite4WF project to a IIS Virtual Directory. copy CSWFInASPNET.dll
to bin folder under ASPNETSIte4WF project folder. then , access to Default page in IE.


/////////////////////////////////////////////////////////////////////////////////////////////
Project Dependency

ASPNETSite4WF -> CSWFInASPNET


/////////////////////////////////////////////////////////////////////////////////////////////
Creation:

To create this sample from scratch, follow these steps.

step 1. Create a Sequential Workflow Library named CSWFInASPNET, name Solution as
CSWFInASPNET too.
step 2. In Project CSWFInASPNET, delete defiantly created Workflow1 and add a new cs
file named DivideNumberWF.cs to this project. and copy the following code to DivideNumberWF.cs
     using System; 
     using System.Collections.Generic; 
     using System.Workflow.Activities; 

     class DivideNumberWF : SequentialWorkflowActivity{ 
         private DelayActivity delayActivity1; 
         private CodeActivity codeActivity1; 

         public double Number1 { getset; } 
         public double Number2 { getset; } 
         public Double Result { getset; } 

         public DivideNumberWF(){ 
             InitializeComponent(); 
         } 

         [System.Diagnostics.DebuggerNonUserCode
         private void InitializeComponent(){ 
             this.CanModifyActivities = true
             this.codeActivity1 = new System.Workflow.Activities.CodeActivity(); 
             this.delayActivity1 = new System.Workflow.Activities.DelayActivity(); 

             // codeActivity1 
             this.codeActivity1.Name = "codeActivity1"
             this.codeActivity1.ExecuteCode += new System.EventHandler(this.codeActivity1_ExecuteCode); 

             // delayActivity1 
             this.delayActivity1.Name = "delayActivity1"
             this.delayActivity1.TimeoutDuration = System.TimeSpan.Parse("00:00:10"); 

             // DivideNumberWF 
             this.Activities.Add(this.delayActivity1); 
             this.Activities.Add(this.codeActivity1); 
             this.Name = "DivideNumberWF"
             this.CanModifyActivities = false
        } 

        private void codeActivity1_ExecuteCode(object sender, EventArgs e) { 
             if (Number2 != 0){ 
                  Result = Number1 / Number2; 
             }else
                  Result = 0;
             } 
         } 
    } 

step 3. Add a ASP.NET Web Application to CSWFInASPNET Solution. Name the Web Application
as ASPNETSite4WF. Add reference to CSWFInASPNET project; also add reference to
System.Workflow.Activities; System.Workflow.ComonentModel; System.Workflow.Runtime
step 4. In ASPNETSiteWF, add Global.asax file, and add the following code to Application_Start method:
     WorkflowRuntime workflowRuntime = new WorkflowRuntime(); 
      // Add ManualWorkflowSchedulerService 
     ManualWorkflowSchedulerService scheduler = new ManualWorkflowSchedulerService(true); 
     workflowRuntime.AddService(scheduler);

     Application["WorkflowRuntime"] = workflowRuntime;
Next, add following code to Application_End method:
      WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"as WorkflowRuntime
     workflowRuntime.StopRuntime(); 
Then, use VS to fix reference.
step 5. Add the following code to Default.aspx.cs file    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Workflow.Runtime;
    using System.Workflow.Runtime.Hosting;
    using System.Threading;
    using CSWFInASPNET;
    namespace ASPNETSite4WF{
        public partial class _Default : System.Web.UI.Page{
             WorkflowRuntime workflowRuntime;
             WorkflowInstance wfInstance;
             Guid instanceId;
             AutoResetEvent autoWaithandler = new AutoResetEvent(false);
             double dividendValue;
             double divisorValue;
             protected void Page_Load(object sender, EventArgs e){}
             protected void Button1_Click(object sender, EventArgs e){
                 workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;
                 //retrieve the scheduler that is used to execute workflows
                 ManualWorkflowSchedulerService scheduler = workflowRuntime.GetService(typeof(ManualWorkflowSchedulerService)) as ManualWorkflowSchedulerService;
                 //handle the WorkflowCompleted event in order to
                 //retrieve the output parameters from the completed workflow
                 workflowRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
                 Double.TryParse(TextBox3.Text, out dividendValue);
                 Double.TryParse(TextBox2.Text, out divisorValue);
                 //pass the input parameters to the workflow
                 Dictionarying, Object> wfArguments = new Dictionary<string, object>();
                 wfArguments.Add("Number1", dividendValue);
                 wfArguments.Add("Number2", divisorValue);

                 //create and start the workflow
                 WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(DivideNumberWF), wfArguments);
                 instance.Start();
                 //execute the workflow synchronously on our thread
                 scheduler.RunWorkflow(instance.InstanceId);
                 autoWaithandler.WaitOne();
            }

            void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e){
                //get the result from the workflow
                if (e.OutputParameters.ContainsKey("Result")){
                    Double quotientValue = (Double)e.OutputParameters["Result"];
                    TextBox4.Text = quotientValue.ToString();
                }
                autoWaithandler.Set();

            }

        }

    } 


Use VS to fix references .

step 6. Copy the following code to Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPNETSite4WF._Default" %> 

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head id="Head1" runat="server"> 
<title>title> 
head> 
<body> 
<form id="form2" runat="server"> 
<div> 
Number1<asp:TextBox ID="TextBox3" runat="server">asp:TextBox> 
<br /> 
Number2<asp:TextBox ID="TextBox2" runat="server">asp:TextBox> 
<br /> 
<asp:Button ID="Button1" runat="server" Text="Divide" 
onclick="Button1_Click" /> 
<br /> 
<br /> 
Result     
<asp:TextBox ID="TextBox4" runat="server">asp:TextBox> 
div> 
form> 
body> 
html






Happy Programming

No comments: