zoukankan      html  css  js  c++  java
  • Workflow Foundation 4.0中的事件驱动流程设计和应用(五)

    之前,我通过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<XName>() { 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<XName>() { 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文件内容

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
        </configSections>
        <connectionStrings>
            <add name="Host.Properties.Settings.WF4ConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=WF4;Integrated Security=True"
                providerName="System.Data.SqlClient" />
        </connectionStrings>
      <system.serviceModel>
    
        
        
        <behaviors>
          <serviceBehaviors>
            <behavior name="WorkflowService">
              <sqlWorkflowInstanceStore connectionStringName="Host.Properties.Settings.WF4ConnectionString"/>
              <workflowIdle timeToPersist="0" timeToUnload="0"/>
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        
        <services>
          <service name="DocumentReviewLib.DocumentReviewWorkflow" behaviorConfiguration="WorkflowService">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/DRS"/>
              </baseAddresses>
            </host>
            <endpoint contract="IDocumentReview" address="" binding="basicHttpBinding"></endpoint>
          </service>
    
          <service name="Host.CommonService">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/Common"/>
              </baseAddresses>
            </host>
    
            <endpoint contract="Host.ICommonService" binding="basicHttpBinding" address=""></endpoint>
          </service>
        </services>
        
        
        
      </system.serviceModel>
      
    </configuration>
     
    这个案例的最终代码范例,请通过 这里 下载
     
     
  • 相关阅读:
    一月份反思内容:BUG & Communicate
    重构一个繁琐的数据结构
    GTD(Get Things Done)
    SortedList<TKey,TValue> 和 SortedDictionary<TKey,TValue>
    对Ado.net Entity Data Model Designer很失望
    A class to clone objects
    collection.All(x=> whatever(x)) 与 collection.Any(x=>whatever(x))
    Asp.net MVC里的TempData是一个整体
    Some tips about ubuntu server | or it may works on other linux distributions too
    每个程序员都必须遵守的编程原则
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1848167.html
Copyright © 2011-2022 走看看