之前,我通过4篇文章介绍了在WF4中开发基于事件的工作流的范例。请参考下面的链接。
Workflow Foundation 4.0中的事件驱动流程设计和应用(一)
Workflow Foundation 4.0中的事件驱动流程设计和应用(二)
Workflow Foundation 4.0中的事件驱动流程设计和应用(三)
Workflow Foundation 4.0中的事件驱动流程设计和应用(四)
这一篇是这个系列的最后一篇,介绍如何通过配置文件,而不是代码的方式启动宿主。这在现实工作中是相当有用的,请大家参考下面的实例。
【注意】有朋友也问到单独用数据库存储业务方面的数据,那是没有错的。一般可以通过自定义的Activity去完成这些操作,都是标准的ADO.NET的数据访问操作。这里就不做展开了。
这个案例的最终代码范例,请通过 这里 下载
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Activities; using System.ServiceModel.Activities; using System.ServiceModel.Description; using System.Activities.DurableInstancing; using System.Runtime.DurableInstancing; using System.Activities.Persistence; using System.ServiceModel.Activities.Description; using System.Xml.Linq; namespace Host { class Program { static void Main(string[] args) { var host = new WorkflowServiceHost( new DocumentReviewLib.DocumentReviewWorkflow(), new Uri("http://localhost:8080/DRS")); host.AddDefaultEndpoints();//这个方法是添加了一些标准的端点 host.Description.Behaviors.Add( new ServiceMetadataBehavior() { HttpGetEnabled = true }); var store = new SqlWorkflowInstanceStore("server=(local)//sqlexpress;database=WF4;integrated security=true"); host.UnknownMessageReceived += (o, e) => { Console.WriteLine("/n" + e.Message + "/n"); }; host.Description.Behaviors.Add( new WorkflowIdleBehavior() { TimeToPersist = TimeSpan.FromSeconds(0) }); XNamespace xNS = XNamespace.Get("http://xizhang.com/DocumentReview"); store.Promote("DocumentReview", new List () { xNS.GetName( "TicketId") }, null); host.WorkflowExtensions.Add( new Extensions.MyInstanceStoreParticpant()); host.DurableInstancingOptions.InstanceStore = store; host.Open(); var common = new ServiceHost( typeof(CommonService), new Uri( "http://localhost:8080/Common")); common.AddServiceEndpoint( typeof(ICommonService).FullName, new BasicHttpBinding(), ""); common.Open(); Console.WriteLine( "Server is ready."); Console.Read(); } } [ServiceContract] public interface ICommonService { [OperationContract] int[] GetTicketIds(); } public class CommonService : ICommonService { public int[] GetTicketIds() { var ctx = new InstanceStoreDataContext(); return ctx.DocumentReviewTasks.Select(r => ( int)r.TicketId).ToArray(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Activities; using System.ServiceModel.Activities; using System.ServiceModel.Description; using System.Activities.DurableInstancing; using System.Runtime.DurableInstancing; using System.Activities.Persistence; using System.ServiceModel.Activities.Description; using System.Xml.Linq; namespace Host { class Program { static void Main(string[] args) { var host = new WorkflowServiceHost( new WorkflowService() { ConfigurationName = "DocumentReviewLib.DocumentReviewWorkflow", Body = new DocumentReviewLib.DocumentReviewWorkflow() }); //这里可以通过进一步的Behavior定制来简化。此处略 XNamespace xNS = XNamespace.Get("http://xizhang.com/DocumentReview"); var store = (SqlWorkflowInstanceStoreBehavior)host.Description.Behaviors.FirstOrDefault(b => b.GetType() == typeof(SqlWorkflowInstanceStoreBehavior)); store.Promote("DocumentReview", new List () { xNS.GetName( "TicketId") }, null); //这里可以通过进一步的Behavior定制来简化。此处略 host.WorkflowExtensions.Add( new Extensions.MyInstanceStoreParticpant()); host.Open(); var common = new ServiceHost( typeof(CommonService)); common.Open(); Console.WriteLine( "Server is ready."); Console.Read(); } } [ServiceContract] public interface ICommonService { [OperationContract] int[] GetTicketIds(); } public class CommonService : ICommonService { public int[] GetTicketIds() { var ctx = new InstanceStoreDataContext(); return ctx.DocumentReviewTasks.Select(r => ( int)r.TicketId).ToArray(); } } }
"1.0" encoding="utf-8" ?> "Host.Properties.Settings.WF4ConnectionString" connectionString= "Data Source=./sqlexpress;Initial Catalog=WF4;Integrated Security=True" providerName= "System.Data.SqlClient" /> "WorkflowService"> "Host.Properties.Settings.WF4ConnectionString"/> "0" timeToUnload= "0"/> "true"/> "DocumentReviewLib.DocumentReviewWorkflow" behaviorConfiguration= "WorkflowService"> "http://localhost:8080/DRS"/> "IDocumentReview" address= "" binding= "basicHttpBinding"> "Host.CommonService"> "http://localhost:8080/Common"/> "Host.ICommonService" binding= "basicHttpBinding" address= "">
这个案例的最终代码范例,请通过 这里 下载