设为首页 收藏本站
查看: 913|回复: 0

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

[复制链接]

尚未签到

发表于 2018-12-11 07:11:58 | 显示全部楼层 |阅读模式
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 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(null, new 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(Dictionary 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(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, true, new 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 parameters = new Dictionary();
   25                 parameters.Add("LeaveId", leaveId);
   26                 parameters.Add("EmployeeInfo", employeeInfo);
   27
   28                 return Factory.CreateWorkflow(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(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





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-649891-1-1.html 上篇帖子: Win2003 下IIS配置PHP+mysql环境 下篇帖子: 重提URL Rewrite(1):IIS与ASP.NET
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表