65.9K
CodeProject 正在变化。 阅读更多。
Home

从 Workflow Foundation 向 ASP.NET 返回数据集

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2011 年 1 月 21 日

CPOL

1分钟阅读

viewsIcon

25706

downloadIcon

457

同步从 Workflow Foundation 向 ASP.NET 返回数据集

引言

下面的代码将帮助您使用属性从 Workflow Foundation 实例同步检索 DataSet 对象。

背景

本文与我们在 Workflow Foundation 中返回 Dataset 所做的所有繁琐的管道工作形成对比。

Using the Code

让我立即开始编写代码。

  • 创建一个空解决方案。向其中添加一个 ASP.NET 项目。
  • 在默认页面上,添加一个 GridView 或您喜欢的任何其他数据绑定控件。
  • 添加另一个项目,WWF 顺序库,到解决方案中。
  • 仅向 WWF 设计器添加一个代码活动。
  • 将 CodeActivity 的处理程序命名为“CallMe”。
  • 打开您的 Workflow1.designer.cs 文件。
  • 紧跟在类名之后,添加一个返回类型为 DataSetpublic 属性,将其命名为 Result
  • 打开 Workflow.cs 文件,在 partial 类中,找到我们定义的 CallMe 方法,将以下代码添加到 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 类。之后,在 config 文件中添加适当的 ConnectionString 部分以检索连接字符串。

  • 编译您的 Workflow 库。
  • 现在切换回您的 ASP.NET 应用程序。
  • 向其中添加 Global.ascx 文件。将以下代码添加到 Application_Start
    // Code that runs on application startup
    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 中。
    // Code that runs on application shutdown
    System.Workflow.Runtime.WorkflowRuntime workflowRuntime 
        = Application["WorkflowRuntime"] as System.Workflow.Runtime.WorkflowRuntime;
    
    workflowRuntime.StopRuntime();
  • 就是这样。打开您的 Default.aspx.cs 文件。
  • 引用您之前创建的顺序 Workflow 库,以及 System.Workflow.RuntimeSystem.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<WorkflowCompletedEventArgs>
    				(workflowRuntime_WorkflowCompleted);
    
                WorkflowInstance instance 
                    = workflowRuntime.CreateWorkflow(typeof(ASPNetWorkflow.Workflow1));
                instance.Start();
    
                //execute the workflow instance synchronously on our thread.
                scheduler.RunWorkflow(instance.InstanceId);
                autoWaitHandler.WaitOne();
            }
            catch (Exception ex)
            {
                Response.Write("Error :" + ex.Message);
            }
        }
    
        void workflowRuntime_WorkflowCompleted
    		(object sender, WorkflowCompletedEventArgs e)
        {
            //Get the result from the workflow.
            if (e.OutputParameters.ContainsKey("Result"))
            {
                DataSet ds = (DataSet)e.OutputParameters["Result"];
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }
                
            autoWaitHandler.Set();
        }
    }

就是全部。将您的 Web 应用程序设置为启动项目并运行它。

历史

  • 2011 年 1 月 21 日:初始发布
© . All rights reserved.