WF的一个ASPNET例子

很多朋友总是问我如何在ASPNET中使用WF
其实我从来不关心前台用什么实现,不管理是B/S还是C/S
手写了一个ASPNET页与WF工作流例子,
这个例子很简单,例子本身并没有什么价值
希望这个例子能给一些朋友代来其他方面的收获

aspx页面代码
<%@ Page Language="C#" %>
<script runat="server">
  
//工作流引擎,使用静态对象,使所有用户都可共享,并在application生命周期存活
public static System.Workflow.Runtime.WorkflowRuntime runtime;

//启动引擎按钮
System.Web.UI.WebControls.Button btStartRuntime = new System.Web.UI.WebControls.Button();

//得到实例列表按钮
System.Web.UI.WebControls.Button btListInstanct = new System.Web.UI.WebControls.Button();
   
//实例列表
System.Web.UI.WebControls.RadioButtonList rlList = new System.Web.UI.WebControls.RadioButtonList();


//创建实例按钮
System.Web.UI.WebControls.Button btCreateInstance = new System.Web.UI.WebControls.Button();

//下一步按钮
System.Web.UI.WebControls.Button btNext = new System.Web.UI.WebControls.Button();

protected void Page_Load(object sender, EventArgs e)
{
if (runtime == null)
{
System.Web.HttpContext.Current.Response.Write(
"引擎没有启动<p>");
}
else
{
System.Web.HttpContext.Current.Response.Write(
"引擎已启动<p>");
}

//
btStartRuntime.Text = "启动/重新启动引擎";
btStartRuntime.Click 
+= new EventHandler(btStartRuntime_Click);
form1.Controls.Add(btStartRuntime);

//
btCreateInstance.Text = "创建实例";
form1.Controls.Add(
new System.Web.UI.LiteralControl("<p>"));
btCreateInstance.Click 
+= new EventHandler(btCreateInstance_Click);
form1.Controls.Add(btCreateInstance);
   
//
form1.Controls.Add(new System.Web.UI.LiteralControl("<p>"));
btListInstanct.Text 
= "得到实例列表";
btListInstanct.Click 
+= new EventHandler(btListInstanct_Click);
form1.Controls.Add(btListInstanct);

//
form1.Controls.Add(new System.Web.UI.LiteralControl("<p>"));
form1.Controls.Add(rlList);

//
form1.Controls.Add(new System.Web.UI.LiteralControl("<p>"));
btNext.Text 
= "下一步";
btNext.Click 
+= new EventHandler(btNext_Click);
form1.Controls.Add(btNext);
}

void btNext_Click(object sender, EventArgs e)
{
if (rlList.SelectedItem != null)
{
System.Workflow.Runtime.WorkflowInstance temp 
= runtime.GetWorkflow(new Guid(rlList.SelectedItem.Text));
temp.Resume();
System.Threading.Thread.Sleep(
1000);
}
}

void btCreateInstance_Click(object sender, EventArgs e)
{
runtime.CreateWorkflow(
typeof(自定义工作流)).Start();
}

void btListInstanct_Click(object sender, EventArgs e)
{
rlList.Items.Clear();
foreach (System.Workflow.Runtime.WorkflowInstance temp in runtime.GetLoadedWorkflows())
{
System.Web.UI.WebControls.ListItem li 
= new ListItem(temp.InstanceId.ToString());
rlList.Items.Add(li);
}

}

void btStartRuntime_Click(object sender, EventArgs e)
{
runtime 
= new System.Workflow.Runtime.WorkflowRuntime();
runtime.Started 
+= new EventHandler<System.Workflow.Runtime.WorkflowRuntimeEventArgs>(runtime_Started);
runtime.WorkflowCreated 
+= new EventHandler<System.Workflow.Runtime.WorkflowEventArgs>(runtime_WorkflowCreated);
runtime.StartRuntime();
}



void runtime_WorkflowCreated(object sender, System.Workflow.Runtime.WorkflowEventArgs e)
{
System.Web.HttpContext.Current.Response.Write(e.WorkflowInstance.InstanceId.ToString() 
+ "实例建立<p>");
}

void runtime_Started(object sender, System.Workflow.Runtime.WorkflowRuntimeEventArgs e)
{
System.Web.HttpContext.Current.Response.Write(
"引擎启动成功<p>");
}


class 自定义工作流:System.Workflow.Activities.SequentialWorkflowActivity 
{
/*
 
 [code1(CodeActivity)]
      ↓
 [su1(SuspendActivity)]
      ↓
 [code2(CodeActivity)]
      ↓
 [su1(SuspendActivity)]
      ↓ 
 [code3(CodeActivity)]
 
 
*/
public 自定义工作流()
{
//
System.Workflow.Activities.CodeActivity code1 = new System.Workflow.Activities.CodeActivity("code1");
code1.ExecuteCode 
+= delegate(object s, EventArgs ee) {};

//   
System.Workflow.ComponentModel.SuspendActivity su1 = new System.Workflow.ComponentModel.SuspendActivity("su1");

//
System.Workflow.Activities.CodeActivity code2 = new System.Workflow.Activities.CodeActivity("code2");
code2.ExecuteCode 
+= delegate(object s, EventArgs ee) { };

//
System.Workflow.ComponentModel.SuspendActivity su2 = new System.Workflow.ComponentModel.SuspendActivity("su2");

//
System.Workflow.Activities.CodeActivity code3 = new System.Workflow.Activities.CodeActivity("code3");
code3.ExecuteCode 
+= delegate(object s, EventArgs ee) {};


//
this.CanModifyActivities = true;
this.Activities.Add(code1);
this.Activities.Add(su1);
this.Activities.Add(code2);
this.Activities.Add(su2);
this.Activities.Add(code3);
this.CanModifyActivities = false
}
}
</script>

<html >
<body>
<form id="form1" runat="server">
</form>
</body>
</html>

Web.Config文件

<?xml version="1.0"?>
<configuration>
<system.web>
<compilation>
<assemblies>
<add assembly="System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Workflow.ComponentModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
</system.web>
</configuration>

保存一下,注意编辑格式,放到IIS里的就可以运行了
-----------------------------------------------------------------------

posted @ 2007-08-27 20:10  WXWinter(冬)  阅读(8886)  评论(16编辑  收藏  举报