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. introcuding less css with less.js, using webcompiler ext

    1.html [watch out for the !js load sequence and the attribute of !rel stylesheet/less!] <!DOCTYPE ...

  2. 根据搜素的字符串改变label包含该字符串的文字

    http://www.2cto.com/kf/201504/391811.html NSString *text =@"人生若只如初见"; //判断字符串所在的位置,并不区分大小写 ...

  3. Python和Ruby抓取网页时的中文乱码问题(在Eclipse和Apatana Studio下均是这种解决方法

    Python抓取中文网页乱码 :Eclipse+pydev2.2+python2.7  :Apatana Studio3+ pydev2.2+python2.7      run时设置 run--&g ...

  4. HDU 1724 Ellipse(数值积分の辛普森公式)

    Problem Description Math is important!! Many students failed in 2+2’s mathematical test, so let's AC ...

  5. WindowsService 安装后报错: 无法启动计算机“.”上的服务 解决方案

    问题 : 根据客户的需求做了一个小程序,需要有对WindowsService 安装,卸载,启动,停止的操作. 编译好之后在我的工程内直接Run 没问题.直接在\bin\Debug 点小程序运行,任何操 ...

  6. spark使用Hive表操作

    spark Hive表操作 之前很长一段时间是通过hiveServer操作Hive表的,一旦hiveServer宕掉就无法进行操作. 比如说一个修改表分区的操作 一.使用HiveServer的方式 v ...

  7. 夺命雷公狗ThinkPHP项目之----企业网站8之栏目的添加完善(无限极分类的完成)

    我们刚才只是完成了添加的一部分,但是我们的上级分类也不能永远都是只有一个死的嘛,所以我们需要对她进行修改: 我们先将add方法里面的数据查出来再说: 然后在模板页进行遍历: 展示效果如下所示: 虽然是 ...

  8. 将服务费用DIY到底----走出软件作坊:三五个人十来条枪 如何成为开发正规军(十)[转]

    前一段时间,讲了一系列开发经理.实施经理.服务经理的工具箱:开发经理的工具箱---走出软件作坊:三五个人十来条枪 如何成为开发正规军(三) ,实施经理的工具箱--走出软件作坊:三五个人十来条枪 如何成 ...

  9. andriod之应用内置浏览器 webview

    参考:http://my.eoe.cn/694183/archive/10476.html http://blog.csdn.net/it_ladeng/article/details/8136534 ...

  10. 评playerc网友的"求比指定数大且最小的“不重复数”问题"

    问题见:对Alexia(minmin)网友代码的评论及对“求比指定数大且最小的‘不重复数’问题”代码的改进 .算法:求比指定数大且最小的“不重复数”问题的高效实现 . playerc网友的代码如下(求 ...