WF终于有了一点入门的感觉

今天做WF 的.NET编程
一直弄了好久,对状态机方式今天终于有了一点小小的入门。还要感谢 曲滨 的程序

asp.net使用wwf
1,配置 web.config
    在 <configuration> 与<system.web>之间需要添加
<configSections>
    
<section name="WorkflowRuntime" type="System.Workflow.Runtime.Configuration.WorkflowRuntimeSection, System.Workflow.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    
<section name="ExternalDataExchangeService" type="System.Workflow.Activities.ExternalDataExchangeServiceSection, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  
</configSections>
 
<appSettings/>
 
<connectionStrings/>
2,在网站运行的时候就加载wf服务,编写 global.asax
System.Workflow.Runtime.WorkflowRuntime WorkflowRuntime = new System.Workflow.Runtime.WorkflowRuntime("WorkflowRuntime");

/*下面几句后面再解释*/
 System.Workflow.Activities.ExternalDataExchangeService dataService 
= new System.Workflow.Activities.ExternalDataExchangeService();
 WorkflowRuntime.AddService(dataService);
 Billdayone.MyBillWorkFlow myBillWorkFlow 
= new Billdayone.MyBillWorkFlow();
 dataService.AddService(myBillWorkFlow);
 
/*上面几句后面再解释*/
Application[
"WorkflowRuntime"= WorkflowRuntime;
 WorkflowRuntime.StartRuntime();

3.接口Interface的理解
以前一直都没怎么接触这个东东,现在觉得接口其实就是一个抽象类,由于知识有限,记住关于状态的Interface写法就好了
[ExternalDataExchange]//一定要写这个
 public interface IBillWorkFlow
 
{
        
event EventHandler<ExternalDataEventArgs> BillSubmit;//自定义状态转移事件
        event EventHandler<ExternalDataEventArgs> BillOk;
 }

//下面照着模式写,依葫芦画票
 [Serializable]
    
public class MyBillWorkFlow : IBillWorkFlow {
        
public MyBillWorkFlow() {
            System.Diagnostics.Debug.WriteLine(
"MyBillWorkFlow Init");
        }

        Dictionary
<string, EventHandler<ExternalDataEventArgs>> _EventList = new Dictionary<string, EventHandler<ExternalDataEventArgs>>();

        
public void RaisEvent(string strName,Guid guidInstanceId) {
            
if (_EventList[strName] != null)
            
{
                
try
                
{
                    EventHandler
<ExternalDataEventArgs> evehandler = _EventList[strName];
                    ExternalDataEventArgs ede 
= new ExternalDataEventArgs(guidInstanceId);
                    evehandler(
this, ede);
                }

                
catch 
                
                }

            }

        }


        
public event EventHandler<ExternalDataEventArgs> BillSubmit {
            add 
{
                System.Diagnostics.Debug.WriteLine(
"add BillSubmit event");
                _EventList.Add(
"BillSubmit", value); 
            }

            remove 
{ _EventList.Remove("BillSubmit"); }
        }


        
public event EventHandler<ExternalDataEventArgs> BillOk {
            add 
{ _EventList.Add("BillOk", value); }
            remove 
{ _EventList.Remove("BillOk"); }
        }

    }

4.再来解释一下global.asax中的那几个东东
 
System.Workflow.Activities.ExternalDataExchangeService dataService = new System.Workflow.Activities.ExternalDataExchangeService();
 WorkflowRuntime.AddService(dataService);
 Billdayone.MyBillWorkFlow myBillWorkFlow 
= new Billdayone.MyBillWorkFlow();
 dataService.AddService(myBillWorkFlow);
//这几个东东是把自己定义的接口类加载到WorkflowRuntime的实例中去,很重要的。

5.接下来就是操作
a.创建一个工作流
System.Workflow.Runtime.WorkflowRuntime workflowruntime = Application["WorkflowRuntime"as System.Workflow.Runtime.WorkflowRuntime;
System.Workflow.Runtime.WorkflowInstance workflowinstance 
=  workflowruntime.CreateWorkflow(typeof(Billdayone.BillWorkFlow));
workflowinstance.Start();
b.得到该WorkflowRuntime的所有正在执行的工作流
System.Workflow.Runtime.WorkflowRuntime workflowruntime = application["WorkflowRuntime"as System.Workflow.Runtime.WorkflowRuntime;
System.Collections.ObjectModel.ReadOnlyCollection
<System.Workflow.Runtime.WorkflowInstance> listWorkflowInstance = workflowruntime.GetLoadedWorkflows();
foreach (System.Workflow.Runtime.WorkflowInstance key in listWorkflowInstance) {
            System.Workflow.Activities.StateMachineWorkflowInstance stateWorkflow 
= new System.Workflow.Activities.StateMachineWorkflowInstance(workflowruntime, key.InstanceId);
            WorkFlowShow 
+= key.InstanceId + " " + stateWorkflow.CurrentState.QualifiedName + " <input type=\"button\" name=\"button\" id=\"button\" value=\"next\" onclick=\"document.getElementById('TextBox2').value='"+key.InstanceId+"'\" />" + "</br>";
        }

c.执行一个工作流,转化其状态
Billdayone.MyBillWorkFlow mMyworkflow = (Application["WorkflowRuntime"as System.Workflow.Runtime.WorkflowRuntime).GetService<Billdayone.MyBillWorkFlow>();

if (mMyworkflow != null)
        
{
            System.Workflow.Activities.StateMachineWorkflowInstance stateWorkflow 
= new System.Workflow.Activities.StateMachineWorkflowInstance(Application["WorkflowRuntime"as System.Workflow.Runtime.WorkflowRuntime, instanceId);
            
if (stateWorkflow.CurrentStateName == "stateInit")
            
{
                mMyworkflow.RaisEvent(
"BillSubmit", instanceId);
            }

            
if (stateWorkflow.CurrentStateName == "stateFinance")
            
{
                mMyworkflow.RaisEvent(
"BillOk", instanceId);
            }


        }

        
else {
            System.Diagnostics.Debug.WriteLine(
"mMyworkflow not found");
        }

好了,只能做到这里,关于如何传参数以及把工作流从内存中保存下来要进一步学些。。。

downloadBilldayone.rar 开发环境 .net2005安装.net framework 3.0安装winfx安装Visual Studio 2005 extensions for .NET Framework 3.0 (Windows Workflow Foundation)
iis 5.1 os winxp
原文地址:https://www.cnblogs.com/poplau/p/1237034.html