`
terryfeng
  • 浏览: 492269 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

WF (Windows Workflow Foundation) 工作流学习(二)一个Asp.Net 与 顺序工作流 结合的例子

阅读更多

程序下载地址:  http://files.cnblogs.com/TerryFeng/WF2.rar

建立一个空的解决方案,向其中加入一个顺序工作流类库和一个Asp.Net Web 应用程序项目,结果如下图

2009-04-13_143252

向asp.Net 程序中,添加引用,“WF”是工作流项目,如图

2009-04-13_143449

向Web.Config 中加入WF的注册(细节下载代码看一下)。

                    <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
                </sectionGroup>
            </sectionGroup>
        </sectionGroup>

        <!--WF注册服务-->
        <section name="WorkflowRuntime" type="System.Workflow.Runtime.Configuration.WorkflowRuntimeSection, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <!--技术WF注册服务-->

    </configSections>
    
    <!--WF注册服务-->

    <WorkflowRuntime Name="WorkflowServiceContainer">
        <Services>
            <add type="System.Workflow.Runtime.Hosting.ManualWorkflowSchedulerService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            <add type="System.Workflow.Activities.ExternalDataExchangeService, System.Workflow.Activities, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            
        </Services>
    </WorkflowRuntime>
    <!--结束注册服务-->


    <appSettings/>
    <connectionStrings/>

 

添加一个全局Global.asax 文件,添加如下代码

protected void Application_Start(object sender, EventArgs e)
       {
           
           WorkflowRuntime workflowRuntime = new WorkflowRuntime("WorkflowRuntime");
           workflowRuntime.StartRuntime();
           Application["WorkflowRuntime"] = workflowRuntime;


       }

       protected void Session_Start(object sender, EventArgs e)
       {
           WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;
           workflowRuntime.StopRuntime();

       }

向Default.aspx中添加3个控件,Textbox,button,label  ,双击Button为其添加单击事件,并在default.aspx.cs 中添加如下代码

 

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.Workflow.ComponentModel;
using System.Workflow.Activities;
namespace AspNet
{
    public partial class _Default : System.Web.UI.Page
    {
        string Result = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            StartWorkflow();
            Label1.Text = Result;
        }

        private void StartWorkflow()
        {
            WorkflowRuntime workflowRuntime = Application["WorkflowRuntime"] as WorkflowRuntime;


            ManualWorkflowSchedulerService scheduler = workflowRuntime.GetService(typeof(ManualWorkflowSchedulerService)) as ManualWorkflowSchedulerService;

            workflowRuntime.WorkflowCompleted += new EventHandler<WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);

            Dictionary<String, Object> wfPara = new Dictionary<string, object>();

            wfPara.Add("Name", TextBox1.Text);

            WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(typeof(WF.Workflow1), wfPara);

            workflowInstance.Start();

            scheduler.RunWorkflow(workflowInstance.InstanceId);
        }

        void workflowRuntime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
        {
            if (e.OutputParameters.ContainsKey("Result"))
            {
                Result = (string)e.OutputParameters["Result"];
            }

        }

    }
}

好了,下面,在WF项目中的Workflow1,拖入一个Code控件,如下图

2009-04-13_144435

为CodeActivety1.添加事件代码,方法名字可以自己取,也可以用默认的,我这里用了一个Exefun的方法名,

2009-04-13_145052

代码如下

 

namespace WF
{
    public sealed partial class Workflow1 : SequentialWorkflowActivity
    {

        public string Name { get; set; }

        public string Result { get; set; }


        public Workflow1()
        {
            InitializeComponent();
        }

        private void ExeFun(object sender, EventArgs e)
        {
            Result = "你好:" + Name;
        }
    }

}

好了,编译一下程序,在TextBox中输入字符串,点击按钮看一下。

2009-04-13_144823

分享到:
评论

相关推荐

    Dropthings综合门户

    1.简介:Dropthings是一个基于.NET3.5技术的Ajax Web Portal,其中使用了LINQ、Windows Workflow Foundation和ASP.NET AJAX等 2.Web Portal和Widgets(部件)架构: 整个应用程序有清晰的三层架构组成,分别是UI层、业务...

    .NETFrameworkv3.5.zip

    .NET Framework 3.5 基于 .NET Framework 3.0 中增加的新功能以增量方式构建,举例来说,这些新功能包括 Windows Workflow Foundation (WF)、Windows Communication Foundation (WCF)、Windows Presentation ...

    微软最新基于SAAS的架构Crab(源代码+演示安装包+文档)

    如:用 Asp.net ajax 1.0、web parts、themes对页面View进行优化,用WCF(Windows Communication Foundation)对Services进行封装,用WF(Windows Workflow Foundation)实现业务流程中的控制流,用SQlServer 2005实现单...

Global site tag (gtag.js) - Google Analytics