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. G面经prepare: X-Straight

    Define “X-Straight” as X cards with consecutive numbers (X >= 3). Determine if the deck can be fu ...

  2. poj 题目分类(3)

    OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094) 初期: 一 ...

  3. Windows 2003/2008更改远程桌面端口脚本

    保存为bat文件,点击运行按提示输入新端口自动完成,直接下载更改远程桌面端口脚本   @echo off color 0a title @@ 修改Windows XP/2003/2008远程桌面服务端 ...

  4. shp图层创建

    IField,IFieldEdit,IFields,IFieldsEdit,IGeometryDef,IGeometryDefEdit接口  (2013-05-06 20:40:27) 转载▼ 标签: ...

  5. SpringMVC配制全局的日期格式

    SpringMVC文件配制: <mvc:annotation-driven> <!-- 处理responseBody 里面日期类型 --> <mvc:message-co ...

  6. [sinatra] Sinatra再入门

    原文URL:http://www.rubycc.com/bbs/topic_detail/86 1.基础代码app.rb require 'rubygems' require 'sinatra/bas ...

  7. 帮初学者改代码——playerc之“练习:求完数问题”(上)

    原文:“练习:求完数问题” 原代码: // #include <stdio.h> #include <stdlib.h> #include <math.h> #de ...

  8. OpenCV 简介

    自版本OpenCV2.2开始,OpenCV库便被划分为多个模块.这些模块编译成库文件后,位于lib文件夹中. opencv_core模块,包含核心功能,尤其是底层数据结构和算法函数. opencv_i ...

  9. 鸟哥的linux私房菜学习记录之bash

    当你对计算机输入一个指令时,bash会将指令传送给核心kernel,核心再去调用相关的程序,启动硬件. 如果直接让用户操作操作系统,可能会造成系统的崩溃,所以操作系统通过应用程序来让用户操作系统即壳程 ...

  10. linux用户栈内核栈的设置---进程的创建: fork/execve【转】

    转自:http://blog.csdn.net/u011279649/article/details/18795547 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 应用层怎 ...