CallExternalMethodActivity实例

CallExternalMethodActivity 活动和 HandleExternalEventActivity 活动可用于与本地服务进行输入和输出通信。 您可以直接使用这些活动进行一般通信,也可以创建 CallExternalMethodActivity 和 HandleExternalEventActivity 类的子类以创建一些活动,这些活动严格绑定到具有 ExternalDataExchangeAttribute 属性的接口上的特定事件和方法。

CallExternalMethodActivity 基类调用由向 WorkflowRuntime 注册的相应本地服务的 InterfaceType 和 MethodName 属性指定的方法。 此调用是使用从绑定位置的 ParameterBindings 集合中收集的参数以同步方式执行的。 如果该方法具有返回值,则会在活动执行完毕前将这些值设置为绑定位置。

1.定义一个接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Activities;
namespace CallExternalMethodActivityExample
{
[ExternalDataExchange]
public interface IExternalMethod
{
void returnName(String name);
}
}


2.定义接口的实现类:

namespace CallExternalMethodActivityExample
{
public class ExternalMethodService:IExternalMethod
{
public void returnName(String name)
{
Console.WriteLine("the returnName method has be runned");
}
}
}


3.向顺序工作流中拖入CallExternalMethod,并为其指定接口和方法。

4.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Workflow.Activities;
namespace CallExternalMethodActivityExample
{
class Program
{
static void Main(string[] args)
{
using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) {waitHandle.Set();};
workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
{
Console.WriteLine(e.Exception.Message);
waitHandle.Set();
};

ExternalDataExchangeService exchangeService = new ExternalDataExchangeService();
workflowRuntime.AddService(exchangeService);

ExternalMethodService emService = new ExternalMethodService();
exchangeService.AddService(emService);
WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(CallExternalMethodActivityExample.Workflow1));
instance.Start();

waitHandle.WaitOne();
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值