使用Workflow Foundation实例同步获取数据集

在Workflow Foundation中,通常需要进行大量的配置工作来返回DataSet。本文将介绍一种简化的方法,通过使用属性来同步获取Workflow Foundation实例的数据集

创建解决方案和项目

首先,创建一个空的解决方案,并添加一个ASP.NET项目。在默认页面上,添加一个GridView或其他喜欢的数据绑定控件。然后,向解决方案中添加另一个项目,名为WWF Sequential Library。在WWF项目中,仅添加一个Code Activity到设计器中,并命名为"CallMe"。

编写代码

接下来,打开Workflow1.designer.cs文件,在类名之后添加一个public属性,返回类型为DataSet,命名为Result。然后在Workflow.cs文件中,找到定义的CallMe方法,并添加以下代码:

string conn = ConfigurationManager.ConnectionStrings["Database"].ConnectionString; SqlConnection sqlcon = new SqlConnection(conn); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter("select top 10 firstname, lastname from Employees", sqlcon); da.Fill(ds); Result = ds;

完成Workflow1.cs类的编写后,需要在配置文件中添加适当的ConnectionString节以检索连接字符串。编译Workflow库。

配置ASP.NET应用程序

然后,切换回ASP.NET应用程序。添加一个Global.ascx文件,并在Application_Start中添加以下代码:

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;

在Application_End中添加以下代码:

System.Workflow.Runtime.WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime; workflowRuntime.StopRuntime();

打开Default.aspx.cs文件,引用之前创建的Workflow顺序库以及System.Workflow.Runtime,System.Workflow.Runtime.Hosting,System.Workflow.Activities; System.Threading, System.Data命名空间。

同步执行工作流实例

复制并粘贴以下代码到代码后台:

public partial class _Default : System.Web.UI.Page { WorkflowRuntime workflowRuntime; AutoResetEvent autoWaitHandler = new AutoResetEvent(false); protected void Page_Load(object sender, EventArgs e) { try { workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime; ManualWorkflowSchedulerService scheduler = workflowRuntime.GetService(typeof(ManualWorkflowSchedulerService)) as ManualWorkflowSchedulerService; workflowRuntime.WorkflowCompleted += new EventHandler(workflowRuntime_WorkflowCompleted); WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(ASPNetWorkflow.Workflow1)); instance.Start(); scheduler.RunWorkflow(instance.InstanceId); autoWaitHandler.WaitOne(); } catch (Exception ex) { Response.Write("Error :" + ex.Message); } } void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e) { if (e.OutputParameters.ContainsKey("Result")) { DataSet ds = (DataSet)e.OutputParameters["Result"]; GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } autoWaitHandler.Set(); } }
沪ICP备2024098111号-1
上海秋旦网络科技中心:上海市奉贤区金大公路8218号1幢 联系电话:17898875485