Sunday, 1 February 2009

Step by Step Guide to Integrate windows Workflow Foundation Business Process flow in Asp.net 3.5

Here is a guide to Integrate windows Workflow Foundation Business Process flow in Asp.net 3.5 C#

Windows workflow foundation is rather a programming model for creating business process workflows. before Jumping on development , you must need to know three things about windows workflow foundation

1. It has a Process Model Designer
2.Runtime Engine & Runtime Services(such as Tracking services of workflow,persistence services to store workflow execution detail in sql database.)
3.Hosting( to host a workflow in some application like Asp.net,Windows Form Application)

As i described earlier windows workflow foundation is a programming model and to integrate a workflow in Asp.net , one might think using workflow's base Libraries .yes, but the most exciting thing is to use that specific process's web service.

How?

Let expose the whole Workflow as a webservice and call that webservice from a Asp.net.
Now it seems interesting. Here is step by step guide to expose a workflow as webservice and calling webservice methods from Asp.net.

1.Open Visual studio 2008( or Visual studio 2005 with .Net 3.0 Installed).
2.Select sequential Workflow Library Template





To expose workflow as webservice , drag & drop two activities on designer
1. WebserviceInputActivity which enable workflow to receive input parameters from webservice(of course we will pass parameters from asp.net)
2. For a WebserviceInputActivity,we need WebserviceOutputActivity as well which returns a value from workflow to the calling method.Its is placed only on designer if there is a return value of the method that is activated in WebserviceinputActivity.

To expose Methods(that contains our workflow logic) as Web Method
Create an Interfacein the current project

To expose workflow as webservice , you need to drag & drop two activities on designer
1. WebserviceInputActivity which enable workflow to receive input parameters from webservice(of course we will pass parameters from asp.net)
2. For a WebserviceInputActivity,we need WebserviceOutputActivity as well which returns a value from workflow to the calling method.Its is placed only on designer if there is a return value of the method that is activated in WebserviceinputActivity.
To Expose workflow methods as Web methods,you need to create interface nad define each method.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NewUserCreationWorkflow
{
interface INewUserRequest
{
string processNewUser(string strfirstName, string strlastName);
}
}
Now in the workflow designer ,open the properties of the webserviceinput activity as it is marked with red icon (it means you to set some properties of it).


  1. Interfacetype-->NewUserCreationWorkflow.INewUserRequest(click next to interfacetype property and dialog window is opened)



2.MethodName->processNewUser
3.IsActiviting->True


4.click next to strfirstname and strlastname, adialog window is opened , click on "Bind to New Member" (to bind new member. these are input parameters and by binding to these parameters designer will create code for these properties).
Normal 0 false false false MicrosoftInternetExplorer4

   1:   public static 
 DependencyProperty webServiceInputActivity1_strfirstName1Property =
   2:   DependencyProperty.Register
   3:  ("webServiceInputActivity1_strfirstName1", typeof(System.String),
 typeof(NewUserCreationWorkflow.Workflow1));
   4:   
   5:  [DesignerSerializationVisibilityAttribute
   6:  (DesignerSerializationVisibility.Visible)]
   7:   
   8:  [BrowsableAttribute(true)]
   9:   
  10:  [CategoryAttribute("Parameters")]
  11:   
  12:  public String webServiceInputActivity1_strfirstName1
  13:   
  14:  {
  15:   
  16:  get
  17:   
  18:  {
  19:   
  20:  return 
((string)
(base.GetValue
(NewUserCreationWorkflow.Workflow1.webServiceInputActivity1_strfirstName1Property)));
  21:   
  22:  }
  23:   
  24:  set
  25:   
  26:  {
  27:   
  28:  base.SetValue
(NewUserCreationWorkflow.Workflow1.webServiceInputActivity1_strfirstName1Property, value);
  29:   
  30:  }
  31:   
  32:  }
  33:   
  34:  public static DependencyProperty
 webServiceInputActivity1_strlastName1Property = DependencyProperty.Register
("webServiceInputActivity1_strlastName1", typeof(System.String), 
typeof(NewUserCreationWorkflow.Workflow1));
  35:   
  36:  [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
  37:   
  38:  [BrowsableAttribute(true)]
  39:   
  40:  [CategoryAttribute("Parameters")]
  41:   
  42:  public String webServiceInputActivity1_strlastName1
  43:   
  44:  {
  45:   
  46:  get
  47:   
  48:  {
  49:   
  50:  return 
 ((string)
(base.GetValue
(NewUserCreationWorkflow.Workflow1.webServiceInputActivity1_strlastName1Property)));
  51:   
  52:  }
  53:   
  54:  set
  55:   
  56:  {
  57:   
  58:  base.SetValue
(NewUserCreationWorkflow.Workflow1.webServiceInputActivity1_strlastName1Property, value);
  59:   
  60:  }
  61:   
  62:  }
  63:   
  64:  private void processNewUser(object sender, EventArgs e)
  65:   
  66:  {
  67:   
  68:  Random r = new Random();
  69:   
  70:  NewUserIDTicket = webServiceInputActivity1_strlastName1 + r.Next(999).ToString();
  71:   
  72:  }
  73:   
  74:  public static DependencyProperty NewUserIDTicketProperty = 
DependencyProperty.Register("NewUserIDTicket", typeof(System.String),  
typeof(NewUserCreationWorkflow.Workflow1));
  75:   
  76:  [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
  77:   
  78:  [BrowsableAttribute(true)]
  79:   
  80:  [CategoryAttribute("Parameters")]
  81:   
  82:  public String NewUserIDTicket
  83:   
  84:  {
  85:   
  86:  get
  87:   
  88:  {
  89:   
  90:  return ((string)(base.GetValue(NewUserCreationWorkflow.Workflow1.NewUserIDTicketProperty)));
  91:   
  92:  }
  93:   
  94:  set
  95:   
  96:  {
  97:   
  98:  base.SetValue
  99:  (NewUserCreationWorkflow.Workflow1.NewUserIDTicketProperty, value);
 100:   
 101:  }
 102:   
 103:  }
 104:   
 105:  }
 106:   
 107:  }
 108:   
 109:   
 110:   
As NewUserIDTicket is return value , we need to place WebServiceOutputActivity.
After Placing, Set properties of WebServiceOutputActivity.




  • InputActivityName->webServiceInputActivity1
  • Sendingoutput->processNewUser
  • ReturnValue->(click next to the property, a dialog box opened click on 'Bind to new member' , enter NewUserIDTicket. )

Now Publish the workflow as webservice.
In solution explorer, Right click on project and select 'Publish as Webservice'.
VS will create web.config,.asmx,.dll files.

Now open asp.net application or create new asp.net application.
here in this case i will integrate this webservice in my e-commerce Asp.net web example.

Add webservice
Create a User interface form.
Add Webform
Place two textboxes , tree labels and a button


On a button click event write this code:

protected void Button1_Click(object sender, EventArgs e)
{
string newid;
NewUserWebservice.Workflow1_WebService _userwebservice = new NewUserWebservice.Workflow1_WebService();
newid= _userwebservice.processNewUser(TextBox1.Text.Trim(),TextBox2.Text.Trim());
Lbluserid.Text =newid;
}

Build the solution and run.


4 comments:

  1. I have placed this code in global.ascx file since it doesnt work
    System.Workflow.Runtime.WorkflowRuntime workflowRuntime =
    new System.Workflow.Runtime.WorkflowRuntime();

    System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService manualService =
    new System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService();
    workflowRuntime.AddService(manualService);

    workflowRuntime.StartRuntime();

    Application["WorkflowRuntime"] = workflowRuntime;

    ReplyDelete
  2. Thanks for countless help.

    ReplyDelete
  3. is this complete? why do you post something that is not complete?

    ReplyDelete
  4. this is a complete article in its own context.

    ReplyDelete