再发一个WF的例子演示
DelayActivity,ListenActivity,CallExternalMethodActivity,HandleExternalEventActivity实现申请,审批,超时否决的演示
使用Winform作为客户端
使用WCF作为客户端与服务器的通信方式
代码很短,很适合正在学习WF,并想快速将WF应用到商业开发的人
例子是在Windwos server 2008 ,VS2008 ,NET3.5下写的
下载 https://files.cnblogs.com/foundation/wxwinter3.rar
应用场景说明:
流程设计
使用说明
1.启动引擎服务
2.客户端提交申请
3.审批者登录
4.审批者审批
5.申请者查看审批结果
引擎服务的全部代码
namespace WorkflowConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DataSet1 ds = new DataSet1();
WorkflowRuntime workflowRuntime = new WorkflowRuntime();
ExternalDataExchangeService exchange = new ExternalDataExchangeService();
workflowRuntime.AddService(exchange);
exchangeEvent obj = new exchangeEvent(ds);
exchange.AddService(obj);
control ctrl = new control(workflowRuntime, obj);
string WCFurl = "http://localhost:567/wxwinter/";
ServiceHost host = new ServiceHost(ctrl, new Uri(WCFurl));
string WCFClassName = "wxdTest";
host.AddServiceEndpoint(typeof(Icontrol), new BasicHttpBinding(), WCFClassName);
host.Open();
Console.WriteLine("引擎已启动");
Console.Read();
}
}
class exchangeEvent : IExternalEvent
{
DataSet1 ds;
public exchangeEvent(DataSet1 v)
{ds = v;}
public event EventHandler<NextStepEventArgs> ApproveEvent;
public void Approve(string gid, string userResult)
{
Guid guid = new Guid(gid);
NextStepEventArgs e = new NextStepEventArgs(guid);
e.result = userResult;
ApproveEvent(null, e);
}
public System.Data.DataSet GetApproverTask(string username)
{
System.Data.DataView dv = new System.Data.DataView(ds.infoTab);
dv.RowFilter = string.Format("[approver]='{0}' and [state] = 1", username);
System.Data.DataSet temp = new System.Data.DataSet();
temp.Tables.Add(dv.ToTable());
return temp;
}
public System.Data.DataSet GetProposerTask(string username)
{
System.Data.DataView dv = new System.Data.DataView(ds.infoTab);
dv.RowFilter = string.Format("[proposer]='{0}'", username);
System.Data.DataSet temp = new System.Data.DataSet();
temp.Tables.Add(dv.ToTable());
return temp;
}
public void Notify(dataform df)
{
System.Data.DataView dv = new System.Data.DataView(ds.infoTab);
dv.RowFilter = string.Format("[guid]='{0}'", df.guid);
switch (df.state)
{
case 1:
DataSet1.infoTabRow r = ds.infoTab.NewinfoTabRow();
r.guid = df.guid;
r.approver = df.approver;
r.content = df.content;
r.proposer = df.proposer;
r.result = df.result;
r.state = df.state;
ds.infoTab.Rows.Add(r);
break;
case 2:
((DataSet1.infoTabRow)dv[0].Row).state = df.state;
((DataSet1.infoTabRow)dv[0].Row).result = df.result;
break;
case 3 :
((DataSet1.infoTabRow)dv[0].Row).state = df.state;
break ;
}
}
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class control : Icontrol
{
WorkflowRuntime workflowRuntime;
exchangeEvent obj;
public control(WorkflowRuntime r, exchangeEvent o)
{
workflowRuntime = r;
obj = o;
}
public void begingWF(string proposer, string approver, string content)
{
Dictionary<string, object> dc = new Dictionary<string, object>();
dataform df = new dataform();
df.approver = approver;
df.content = content;
df.proposer = proposer;
df.result = "";
dc.Add("DF", df);
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1), dc);
instance.Start();
}
public void Approve(string guid, string result)
{obj.Approve(guid, result);}
public System.Data.DataSet GetApproverTask(string username)
{ return obj.GetApproverTask(username);}
public System.Data.DataSet GetProposerTask(string username)
{ return obj.GetProposerTask(username);}
}
[ServiceContract]
public interface Icontrol
{
[OperationContract]
void Approve(string guid, string result);
[OperationContract]
void begingWF(string proposer, string approver, string content);
[OperationContract]
System.Data.DataSet GetApproverTask(string username);
[OperationContract]
System.Data.DataSet GetProposerTask(string username);
}
}
{
class Program
{
static void Main(string[] args)
{
DataSet1 ds = new DataSet1();
WorkflowRuntime workflowRuntime = new WorkflowRuntime();
ExternalDataExchangeService exchange = new ExternalDataExchangeService();
workflowRuntime.AddService(exchange);
exchangeEvent obj = new exchangeEvent(ds);
exchange.AddService(obj);
control ctrl = new control(workflowRuntime, obj);
string WCFurl = "http://localhost:567/wxwinter/";
ServiceHost host = new ServiceHost(ctrl, new Uri(WCFurl));
string WCFClassName = "wxdTest";
host.AddServiceEndpoint(typeof(Icontrol), new BasicHttpBinding(), WCFClassName);
host.Open();
Console.WriteLine("引擎已启动");
Console.Read();
}
}
class exchangeEvent : IExternalEvent
{
DataSet1 ds;
public exchangeEvent(DataSet1 v)
{ds = v;}
public event EventHandler<NextStepEventArgs> ApproveEvent;
public void Approve(string gid, string userResult)
{
Guid guid = new Guid(gid);
NextStepEventArgs e = new NextStepEventArgs(guid);
e.result = userResult;
ApproveEvent(null, e);
}
public System.Data.DataSet GetApproverTask(string username)
{
System.Data.DataView dv = new System.Data.DataView(ds.infoTab);
dv.RowFilter = string.Format("[approver]='{0}' and [state] = 1", username);
System.Data.DataSet temp = new System.Data.DataSet();
temp.Tables.Add(dv.ToTable());
return temp;
}
public System.Data.DataSet GetProposerTask(string username)
{
System.Data.DataView dv = new System.Data.DataView(ds.infoTab);
dv.RowFilter = string.Format("[proposer]='{0}'", username);
System.Data.DataSet temp = new System.Data.DataSet();
temp.Tables.Add(dv.ToTable());
return temp;
}
public void Notify(dataform df)
{
System.Data.DataView dv = new System.Data.DataView(ds.infoTab);
dv.RowFilter = string.Format("[guid]='{0}'", df.guid);
switch (df.state)
{
case 1:
DataSet1.infoTabRow r = ds.infoTab.NewinfoTabRow();
r.guid = df.guid;
r.approver = df.approver;
r.content = df.content;
r.proposer = df.proposer;
r.result = df.result;
r.state = df.state;
ds.infoTab.Rows.Add(r);
break;
case 2:
((DataSet1.infoTabRow)dv[0].Row).state = df.state;
((DataSet1.infoTabRow)dv[0].Row).result = df.result;
break;
case 3 :
((DataSet1.infoTabRow)dv[0].Row).state = df.state;
break ;
}
}
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class control : Icontrol
{
WorkflowRuntime workflowRuntime;
exchangeEvent obj;
public control(WorkflowRuntime r, exchangeEvent o)
{
workflowRuntime = r;
obj = o;
}
public void begingWF(string proposer, string approver, string content)
{
Dictionary<string, object> dc = new Dictionary<string, object>();
dataform df = new dataform();
df.approver = approver;
df.content = content;
df.proposer = proposer;
df.result = "";
dc.Add("DF", df);
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Workflow1), dc);
instance.Start();
}
public void Approve(string guid, string result)
{obj.Approve(guid, result);}
public System.Data.DataSet GetApproverTask(string username)
{ return obj.GetApproverTask(username);}
public System.Data.DataSet GetProposerTask(string username)
{ return obj.GetProposerTask(username);}
}
[ServiceContract]
public interface Icontrol
{
[OperationContract]
void Approve(string guid, string result);
[OperationContract]
void begingWF(string proposer, string approver, string content);
[OperationContract]
System.Data.DataSet GetApproverTask(string username);
[OperationContract]
System.Data.DataSet GetProposerTask(string username);
}
}
补充一点,对于流程结节的控制,我更喜欢用规则实现,具体见
规则引擎 http://www.cnblogs.com/foundation/archive/2007/08/18/860911.html
这个例子