WF事件驱动(5)

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

 

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

【注意】有朋友也问到单独用数据库存储业务方面的数据,那是没有错的。一般可以通过自定义的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>
 
这个案例的最终代码范例,请通过 这里 下载

posted on 2011-09-03 16:58  Mayvar  阅读(1032)  评论(1编辑  收藏  举报

导航