综合应用WPF/WCF/WF/LINQ之五:将Workflow使用WCF技术Host到IIS中,并实现调用

简介:
1、在设计Workflow时,我们用到了HandleExternalEventActivity和CallExternalMethodActivity,它们引用了ILeaveInterface。因此我们需要实现这个Interface中的事件和方法。
  在Eallies.OA.Workflow.Handler项目中添加一个Class类,并让其继承与ILeaveInterface。
    1  using System;
    2  using System.Collections.Generic;
    3  using System.Linq;
    4  using System.Text;
    5  using Eallies.OA.Workflow.Args;
    6  using Eallies.OA.Workflow.Interface;
    7  using Eallies.OA.Info;
    8  using Eallies.OA.Info.Enum;
    9  using Eallies.OA.Service.Wrapper;
   10 
   11  namespace Eallies.OA.Workflow.Handler
   12 {
   13      public  class  LeaveHandler :  ILeaveInterface
   14     {
   15          #region ILeaveInterface Members
   16 
   17          public  event  EventHandler< LeaveArgs> LeaveApprove;
   18 
   19          public  void UpdateLeaveApproverByLeaveId( int leaveId,  int leaveApprover)
   20         {
   21              LeaveContractClient client =  new  LeaveContractClient();
   22 
   23              try
   24             {
   25                 client.UpdateLeaveApproverByLeaveId(leaveId, leaveApprover);
   26             }
   27              catch
   28             {
   29                  throw;
   30             }
   31              finally
   32             {
   33                 client.Close();
   34             }
   35         }
   36 
   37          public  void UpdateLeaveStatusByLeaveId( int leaveId,  LeaveStatusEnum leaveStatus)
   38         {
   39              LeaveContractClient client =  new  LeaveContractClient();
   40 
   41              try
   42             {
   43                 client.UpdateLeaveStatusByLeaveId(leaveId, leaveStatus);
   44             }
   45              catch
   46             {
   47                  throw;
   48             }
   49              finally
   50             {
   51                 client.Close();
   52             }
   53         }
   54 
   55          #endregion
   56 
   57          public  void RaiseLeaveApprove( Guid instanceId,  LeaveApproveResultEnum leaveApproveResult,  EmployeeInfo employeeInfo)
   58         {
   59              try
   60             {
   61                  if ( this.LeaveApprove !=  null)
   62                 {
   63                      this.LeaveApprove( nullnew  LeaveArgs(instanceId, leaveApproveResult, employeeInfo));
   64                 }
   65             }
   66              catch
   67             {
   68                  throw;
   69             }
   70         }
   71     }
   72 }
  2、外部程序与Workflow的交互有两个场合,一个是CreateWorkflow时,另一个是处理外部事件时。由于Workflow的等待状态可能持续很长时间,甚至中间可能重新启动了机器,因此这两个场合都需要重新建立Workflow运行的环境。
  同时,由于重新启动了机器,内存中的Workflow实例就会丢失。为了防止实例丢失,我们需要将Workflow的实例持久化到数据库中。为了实现这点,可以在Workflow运行时环境中加入SqlWorkflowPersistenceService服务,并设置一旦Workflow空闲了,则持久化到数据库中。
    1  using System;
    2  using System.Collections.Generic;
    3  using System.Linq;
    4  using System.Text;
    5  using System.Web.Configuration;
    6  using System.Workflow.Runtime;
    7  using System.Workflow.Runtime.Hosting;
    8  using System.Workflow.Activities;
    9  using System.Collections;
   10 
   11  namespace Eallies.OA.Workflow.Service
   12 {
   13      public  class  Factory
   14     {
   15          private  static  string _ConnectionString =  WebConfigurationManager.ConnectionStrings[ "Eallies.OA.Workflow"].ConnectionString;
   16          private  static  WorkflowRuntime _WorkflowRuntime =  null;
   17          private  static  WorkflowInstance _WorkflowInstance =  null;
   18          private  static  ExternalDataExchangeService _ExternalDataExchangeService =  null;
   19          private  static  object _Lock =  new  object();
   20 
   21          public  static  Guid CreateWorkflow<TWorkflow, THandler>( Dictionary< stringobject> parameters)
   22         {
   23              try
   24             {
   25                  lock (_Lock)
   26                 {
   27                     GetWorkflowRuntime();
   28 
   29                     _WorkflowInstance = _WorkflowRuntime.CreateWorkflow( typeof(TWorkflow), parameters);
   30 
   31                      if (_ExternalDataExchangeService ==  null)
   32                     {
   33                         _ExternalDataExchangeService =  new  ExternalDataExchangeService();
   34 
   35                         _WorkflowRuntime.AddService(_ExternalDataExchangeService);
   36                     }
   37 
   38                      if (_ExternalDataExchangeService.GetService( typeof(THandler)) ==  null)
   39                     {
   40                         _ExternalDataExchangeService.AddService((THandler) Activator.CreateInstance( typeof(THandler)));
   41                     }
   42 
   43                     _WorkflowInstance.Start();
   44                 }
   45 
   46                  return _WorkflowInstance.InstanceId;
   47             }
   48              catch
   49             {
   50                  throw;
   51             }
   52         }
   53 
   54          public  static T GetHandler<T>( Guid instanceId)
   55         {
   56              try
   57             {
   58                  lock (_Lock)
   59                 {
   60                     GetWorkflowRuntime();
   61 
   62                     _WorkflowInstance = _WorkflowRuntime.GetWorkflow(instanceId);
   63 
   64                      if (_ExternalDataExchangeService ==  null)
   65                     {
   66                         _ExternalDataExchangeService =  new  ExternalDataExchangeService();
   67 
   68                         _WorkflowRuntime.AddService(_ExternalDataExchangeService);
   69                     }
   70 
   71                      if (_ExternalDataExchangeService.GetService( typeof(T)) ==  null)
   72                     {
   73                         _ExternalDataExchangeService.AddService((T) Activator.CreateInstance( typeof(T)));
   74                     }
   75 
   76                      return (T)_ExternalDataExchangeService.GetService( typeof(T));
   77                 }
   78             }
   79              catch
   80             {
   81                  throw;
   82             }
   83         }
   84 
   85          public  static  void GetWorkflowRuntime()
   86         {
   87              try
   88             {
   89                  lock (_Lock)
   90                 {
   91                      if (_WorkflowRuntime ==  null)
   92                     {
   93                          AppDomain.CurrentDomain.ProcessExit +=  new  EventHandler(StopWorkflowRuntime);
   94                          AppDomain.CurrentDomain.DomainUnload +=  new  EventHandler(StopWorkflowRuntime);
   95 
   96                         _WorkflowRuntime =  new  WorkflowRuntime();
   97 
   98                         _WorkflowRuntime.AddService( new  SqlWorkflowPersistenceService(_ConnectionString,  truenew  TimeSpan(0, 0, 0, 10, 0),  new  TimeSpan(0, 0, 0, 10, 0)));
   99 
  100                         _WorkflowRuntime.StartRuntime();
  101                     }
  102                 }
  103             }
  104              catch
  105             {
  106                  throw;
  107             }
  108         }
  109 
  110          private  static  void StopWorkflowRuntime( object sender,  EventArgs e)
  111         {
  112              try
  113             {
  114                  if (_WorkflowRuntime !=  null)
  115                 {
  116                      if (_WorkflowRuntime.IsStarted ==  true)
  117                     {
  118                         _WorkflowRuntime.StopRuntime();
  119                     }
  120                 }
  121             }
  122              catch
  123             {
  124                  throw;
  125             }
  126         }
  127     }
  128 }
  上面的代码中,由于WorkflowRuntime只允许一个实例,且不应该丢失,因此我们采用静态变量的方式保存。
  3、由于上述方法都是静态方法,调用起来就非常简单。
    1  using System;
    2  using System.Collections.Generic;
    3  using System.Linq;
    4  using System.Text;
    5  using System.Workflow.Runtime;
    6  using System.Workflow.Activities;
    7  using Eallies.OA.Workflow;
    8  using Eallies.OA.Workflow.Args;
    9  using Eallies.OA.Workflow.Handler;
   10  using Eallies.OA.Workflow.Service.Contract;
   11  using Eallies.OA.Info;
   12  using Eallies.OA.Info.Enum;
   13 
   14  namespace Eallies.OA.Workflow.Service
   15 {
   16      public  class  LeaveService :  ILeaveContract
   17     {
   18          #region ILeaveContract Members
   19 
   20          public  Guid CreateWorkflow( int leaveId,  EmployeeInfo employeeInfo)
   21         {
   22              try
   23             {
   24                  Dictionary< stringobject> parameters =  new  Dictionary< stringobject>();
   25                 parameters.Add( "LeaveId", leaveId);
   26                 parameters.Add( "EmployeeInfo", employeeInfo);
   27 
   28                  return  Factory.CreateWorkflow< LeaveWorkflowLeaveHandler>(parameters);
   29             }
   30              catch
   31             {
   32                  throw;
   33             }
   34         }
   35 
   36          public  void LeaveApprove( Guid instanceId,  LeaveApproveResultEnum leaveApproveResult,  EmployeeInfo employeeInfo)
   37         {
   38              try
   39             {
   40                  Factory.GetHandler< LeaveHandler>(instanceId).RaiseLeaveApprove(instanceId, leaveApproveResult, employeeInfo);
   41             }
   42              catch
   43             {
   44                  throw;
   45             }
   46         }
   47 
   48          #endregion
   49     }
   50 }
  4、上述代码继承于ILeaveContract,是WCF技术中的契约部分,然后我们只需要在Eallies.OA.Workflow.Service.Host项目中加入一个SVC项目,即可实现把Workflow给Host到IIS中了。
    1  <% @  ServiceHost  Language ="C#"  Debug ="true"  Service ="Eallies.OA.Workflow.Service.LeaveService"  %>




本文转自 Eallies 51CTO博客,原文链接:http://blog.51cto.com/eallies/79047,如需转载请自行联系原作者

目录
相关文章
|
6月前
|
缓存 C# 虚拟化
WPF列表性能提高技术
WPF数据绑定系统不仅需要绑定功能,还需要能够处理大量数据而不会降低显示速度和消耗大量内存,WPF提供了相关的控件以提高性能,所有继承自`ItemsControl`的控件都支持该技术。
|
8月前
|
C# Windows
WPF技术之图形系列Polygon控件
WPF Polygon是Windows Presentation Foundation (WPF)框架中的一个标记元素,用于绘制多边形形状。它可以通过设置多个点的坐标来定义多边形的形状,可以绘制任意复杂度的多边形。
450 0
|
8月前
|
XML C# 数据格式
WPF技术之xmlns
在WPF中,xmlns是XML命名空间(XML Namespace)的缩写,用于引入不同的XML命名空间,以便在XAML文件中使用特定的命名空间中的元素、属性和类型。
215 1
|
8月前
|
C# Windows
WPF技术之RichTextBox控件
WPF RichTextBox是Windows Presentation Foundation (WPF)中提供的一个强大的文本编辑控件,它可以显示富文本格式的文本,支持多种文本处理操作。
344 0
|
8月前
|
存储 自然语言处理 C#
WPF技术之Binding
WPF(Windows Presentation Foundation)是微软推出的一种用于创建应用程序用户界面的框架。Binding(绑定)是WPF中的一个重要概念,它用于在界面元素和数据源之间建立关联。通过Binding,可以将界面元素(如文本框、标签、列表等)与数据源(如对象、集合、属性等)进行绑定,从而实现数据的双向传递和同步更新。
142 2
WPF技术之Binding
|
8月前
|
数据挖掘 数据处理 C#
WPF技术之DataGrid控件
WPF DataGrid是一种可以显示和编辑数据的界面控件。它可以作为表格形式展示数据,支持添加、删除、修改、排序和分组操作。
173 0
|
8月前
|
C#
WPF技术之Visibility
WPF中的Visibility属性用于控制元素在界面上的可见性。
317 1
|
7月前
|
C#
WPF技术之动画系列-上下运动
本例子展现动画小球上下循环运动
125 0
|
8月前
|
前端开发 C# 容器
WPF技术之Command
WPF Command是一种在MVVM(Model-View-ViewModel)模式中用于处理用户界面交互的机制。它可以将用户界面事件(如按钮点击、菜单项选择等)与应用程序逻辑(命令处理)解耦,同时提供了一种便捷的方式来管理和执行命令
131 2
|
8月前
|
定位技术 C# UED
WPF技术之ScrollViewer控件
WPF ScrollViewer是WPF中常用的一个控件,它提供了滚动视图的功能,可用于显示超出容器可视区域的内容。ScrollViewer通常用于容纳大量内容的控件,以在有限的空间内显示这些内容,并允许用户通过滚动来查看隐藏的部分。
690 0