CallExternalMethod用于工作流向宿主进程中通信 简单的介绍一下它的使用技巧,参照网上的一个questioner源码进行了改进,因为我感觉这个源码提供的通信demo过于繁琐。

看看service代码

namespace QuestionService
{
    [ExternalDataExchange]
    public interface ITestService
    {
        void SendResponseDataToHost(TestPara responses);
    }
}
TestPara对象是对参数的一个封装,因为按照我的测试,在调用接口方法中,方法的参数如果不是对象类型,就会无法解决工作流传参的问题。
namespace QuestionService
{
    [Serializable]
    public class TestArgs : ExternalDataEventArgs
    {

        protected TestPara _dataValue = null;

        public TestPara Responses
        {
            get { return _dataValue; }
        }

        public TestArgs(Guid instanceId, TestPara responses)
            : base(instanceId)
        {
            _dataValue = responses;
        }

    }

    [Serializable]
    public class TestPara
    {
        protected bool[] _dataValue = null;

        public bool[] Responses
        {
            get { return _dataValue; }
            set { _dataValue = value; }
        }

    }
}

这是实现的关键地方,DataAvailable事件用于向宿主进程暴露

namespace QuestionService
{
    [Serializable]
    public class TestService : ITestService
    {

        #region ITestService 成员

        public event EventHandler<TestArgs> DataAvailable;

        public void SendResponseDataToHost(TestPara responses)
        {
            if (DataAvailable != null)
            {
                try
                {
                    DataAvailable(this, new TestArgs(WorkflowEnvironment.WorkflowInstanceId, responses));
                }
                catch (Exception ex)
                {

                }
            } // if
        }

        #endregion
    }
}

在工作流中  要设置相关的接口协议,选择调用的方法

需要注意的是 参数要在工作流中设置参数的属性 以供其选择,其中_response是工作流活动返回的数据,这里通过TestPara属性进行封装获取

        private bool[] _response = null;

        private TestPara _TestPara = new TestPara();
        public TestPara TestPara
        {
            get {
                _TestPara.Responses = _response;
                return _TestPara;
            }
        }

最后是宿主调用的过程  对调用进行了改动 DataAvailable事件暴露给宿主进程。以供工作流进程调用。

        private void cmdExecute_Click(object sender, EventArgs e)
        {
            // Disable the execute button
            cmdExecute.Enabled = false;

            // Clear the indicators
            ClearIndicators();

            // Set the cursor to "app starting"
            Cursor = Cursors.AppStarting;

            // Process the request, starting by creating the parameters
            Dictionary<string, object> parms = new Dictionary<string, object>();
            parms.Add();
            ];
            questions[] = tbQuestion1.Text;
            questions[] = tbQuestion2.Text;
            questions[] = tbQuestion3.Text;
            parms.Add("Questions", questions);

            // Create instance.
            _workflowInstance = _workflowRuntime.CreateWorkflow(typeof(QuestionFlow.Workflow1), parms);
            if (_dataExchangeService==null)
            {
             _dataExchangeService = new ExternalDataExchangeService();
            _workflowRuntime.AddService(_dataExchangeService);
            TestService _testService = new TestService();
            _testService.DataAvailable += new EventHandler<QuestionService.TestArgs>(dataService_DataAvailable);
            _dataExchangeService.AddService(_testService);
            }

            // Hook returned data event
            //QuestionService.WorkflowResponseDataService dataService = QuestionService.WorkflowResponseDataService.CreateDataService(_workflowInstance.InstanceId, _workflowRuntime);

            // Start instance.
            _workflowInstance.Start();
        }

        public void dataService_DataAvailable(object sender, QuestionService.TestArgs e)
        {
            IAsyncResult result = this.BeginInvoke(
                new EventHandler(
                       delegate
                       {
                           // Retrieve connection.
                           //QuestionService.WorkflowResponseDataService dataService = QuestionService.WorkflowResponseDataService.GetRegisteredWorkflowDataService(e.InstanceId);

                           // Read the response data
                           bool[] responses = e.Responses.Responses;

                           // Bind the vehicles list to the vehicles table
                           SetIndicators(responses);
                       } // delegate
                    ), null, null
            ); // BeginInvoke
            this.EndInvoke(result);

            // Reset for next request
            WorkflowCompleted();
        }

        private delegate void WorkflowCompletedDelegate();

        private void WorkflowCompleted()
        {
            IAsyncResult result = this.BeginInvoke(
               new EventHandler(
                      delegate
                      {
                          // Reset the cursor
                          Cursor = Cursors.Arrow;

                          // Enable the execute button
                          cmdExecute.Enabled = true;
                      } // delegate
                   ), null, null
           ); // BeginInvoke
            this.EndInvoke(result);

        }

WF3.0 CallExternalMethod使用技巧的更多相关文章

  1. VC6.0实用小技巧

    VC6.0的若干实用小技巧 .检测程序中的括号是否匹配 把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键 “Ctrl+]”.如果括号匹配正确, ...

  2. nyoj--84--阶乘的0(数学技巧)

    阶乘的0 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 计算n!的十进制表示最后有多少个0 输入 第一行输入一个整数N表示测试数据的组数(1<=N<=100 ...

  3. 0.0.Pycharm使用技巧

    调整自动字体大小 Increase(字体变大) Decrease(字体变小) 背景颜色设置 pycharm 左侧菜单问题 解决 pycharm中配置启动Django项目 1.先打开mange.py,然 ...

  4. arcgis中nodata设为0及其小技巧

    一.arcgis中nodata设为0 两个栅格进行叠加,有时会有一部分没有数据,即用identify点击该区域,Value为NoDat a,而不是像其他非空区域一样有值. 此时注意nodata区域要赋 ...

  5. Android 4.0的图形硬件加速及绘制技巧

    转:http://zuiniuwang.blog.51cto.com/3709988/721798 从Android 3.0开始,Android 2D的绘制流程就设计为能够更好地支持硬件加速.使用GP ...

  6. 前端读者 | 关于存储及CSS的一些技巧

    @羯瑞 HTML5存储 cookies 大小限制4K 发送在http请求头中 子域名能读取主域名的cookies 本地存储 localStorage sessionStorage 大小限制5M(注意超 ...

  7. cssfloat布局以及其他小技巧

    css float 布局以及其他小技巧总结 这篇博文 前面四个部分是关于css 经典布局 如果你已经知道了 可以直接跳过看第六部分 css 的其他小技巧 1.0 左右居中布局 <!DOCTYPE ...

  8. TensorFlow2.0使用方法

    TensorFlow2.0 1 使用技巧 更新到最新版本: pip install --upgrade tensorflow pip install --upgrade tensorflow-gpu ...

  9. 利用Shell脚本将MySQL表中的数据转化为json格式

    脚本如下: #!/bin/bash mysql -s -phello test >.log <<EOF desc t1; EOF lines="concat_ws(',', ...

随机推荐

  1. Summary: Merge Sort of Array && 求逆序对

    常用算法(后面有inplace版本): package ArrayMergeSort; import java.util.Arrays; public class Solution { public ...

  2. 二分多重匹配(HDU5093)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  3. 创建一个web Test Plan

    1.添加ThreadGroup (1).线程组界面解析: 线程数:虚拟用户的个数 Ramp-up Period:开启每个用户的延迟时间,如果有5个虚拟用户,Ramp-up Period值是5,Jmet ...

  4. 当执行php脚本时用户关闭浏览器会发生什么?

    2008年8月16日 1,152 views 发表评论 阅读评论 如果一段php脚本执行插入数据到mysql的操作. 一般情况下,由于php脚本在服务器上执行,此时用户虽然关闭了浏览器,但是服务器端的 ...

  5. 夺命雷公狗ThinkPHP项目之----企业网站9之栏目的列表完善(无限极分类的完成)

    我们刚才已经写好model了,那么这里直接来调用下他即可: public function lists(){ $mod = D('Category')->catTree(); $this -&g ...

  6. SSAS计算列如果是中文名称时,必须要在名字外加中括号

    在SSAS中建计算列的时候,如果你给计算列起的是中文名字,一定记住要在名字外加中括号,比如下面这个例子中我们建了一个叫 客服流失数 的计算列 下面图中没有在计算列名称上加中括号这是错误的,因为使用中文 ...

  7. Openstack的HA解决方案【mysql集群配置】

    使用mysql的galera做多主集群配置,galera的集群优势网络上面有对比,这里不在叙述. 1. 新建3台虚拟机(centos6.5) node1:172.17.44.163 node2:172 ...

  8. SDK Manager failed to install 'java.exe' locking directory

    转自:http://stackoverflow.com/questions/13587478/sdk-manager-failed-to-install-java-exe-locking-direct ...

  9. jquery选择器中两个class是什么意思?

    jquery选择器中两个class是什么意思? $(".class1 .class2") 选择class1元素下class2的元素(中间有空格)$(".class1.cl ...

  10. Microsoft Office 2013 Product Key

    Microsoft Office 2013 Product Key ( Professional Plus ) PGD67-JN23K-JGVWW-KTHP4-GXR9G B9GN2-DXXQC-9D ...