Workflow Foundation 4.0中的事件驱动流程设计和应用(五)

本文展示如何使用配置文件而非代码来启动WF4工作流宿主,并提供了具体的代码示例和配置文件内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前,我通过4篇文章介绍了在WF4中开发基于事件的工作流的范例。请参考下面的链接。

Workflow Foundation 4.0中的事件驱动流程设计和应用(一)

Workflow Foundation 4.0中的事件驱动流程设计和应用(二)

Workflow Foundation 4.0中的事件驱动流程设计和应用(三)

Workflow Foundation 4.0中的事件驱动流程设计和应用(四)

 

这一篇是这个系列的最后一篇,介绍如何通过配置文件,而不是代码的方式启动宿主。这在现实工作中是相当有用的,请大家参考下面的实例。

【注意】有朋友也问到单独用数据库存储业务方面的数据,那是没有错的。一般可以通过自定义的Activity去完成这些操作,都是标准的ADO.NET的数据访问操作。这里就不做展开了。

 

这个案例的最终代码范例,请通过 这里 下载

1.修改之前的Host代码

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();
        }
    }

}

 

2. 修改之后的Host代码(请大家比较一下有何区别)

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();
        }
    }

}

 

3.添加的app.config文件内容


 "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=
      "">
     
      
    
    
   
    
    
    
  
  
  

 
 
这个案例的最终代码范例,请通过 这里 下载
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值