綜合運用 WF4 持久化、追蹤、自訂擴充、WCF Workflow Service、WorkflowServiceHost、Interop 呼叫 WF3 規則引擎,打造任務佇列系統:提交請求分派至四大隊列 (Product/Service/Marketing/General),支援 FIFO 或清單選取處理、轉派、取消、超時持久化。
<sqlWorkflowInstanceStore
connectionStringName="Request"
instanceCompletionAction="DeleteAll"
instanceLockedExceptionAction="NoRetry"
instanceEncodingOption="GZip"
hostLockRenewalPeriod="00:00:30" />
<workflowIdle timeToUnload="00:00:10" timeToPersist="00:00:05" />
以 DBConnection 為例,註冊至 WorkflowServiceHost.WorkflowExtensions,流程中透過 context.GetExtension<DBConnection>() 取得連線字串。
繼承 TrackingParticipant,覆寫 Track 解析 CustomTrackingRecord 寫入 QueueTrack 資料表。
public sealed class CreateRequest : CodeActivity
{
public InArgument<string> RequestType { get; set; }
public InArgument<string> UserName { get; set; }
public InArgument<string> UserEmail { get; set; }
public InArgument<string> Comment { get; set; }
public InArgument<Guid> QueueInstanceKey { get; set; }
public InArgument<Guid> RequestKey { get; set; }
protected override void Execute(CodeActivityContext context)
{
var ext = context.GetExtension<DBConnection>();
if (ext == null) throw new InvalidProgramException("No connection string");
var dc = new RequestDataContext(ext.ConnectionString);
var r = new Request { ... };
var persist = context.GetExtension<PersistRequest>();
persist.AddRequest(r);
}
}
本範例改編自 Beginning WF: Windows Workflow in .NET 4.0 最後一章。