---
title: "坚持学习WF(8):本地服务之调用外部方法"
author: "carysun"
date: "2008-05-09T08:17:00Z"
tags: []
slug: "wf-8"
draft: false
lastmod: "2008-05-09T08:17:00Z"
id: 463296
url: "https://carysun.iteye.com/blog/463296"
---

WF提供了一组核心服务，例如在SQL 数据库中存储工作流实例的执行详细信息的持久性服务，计划服务，事务服务和跟踪服务。除了这些WF也提供了另外一种服务，叫做Local Service也可以叫做Data exchange service。主要是实现工作流和宿主程序之间的通信，使工作流能够使用方法和事件通过消息与外部系统交互。 事件用于将数据发送到工作流，而工作流使用方法将数据发送到主机应用程序。 通过事件与工作流进行通信的功能提供了一种将数据发送到工作流的异步方式。本文主要讲述调用外部方法的部分。

下图说明本地通信服务如何与其主机应用程序通信：

[![01](/images/cnblogs_com/carysun/WindowsLiveWriter/WF8_614C/01_thumb.jpg)](/images/cnblogs_com/carysun/WindowsLiveWriter/WF8_614C/01.jpg)

下面首先说说如何开发一个本地服务：

1.使用C#的接口定义服务契约，在接口中定义你方法和事件。并使用\[ExternalDataExchangeAttribute]装饰该接口，用于说明这是一个本地服务的接口。  
2.开发一个实现了该接口的类，用于实现你的逻辑。  
3.创建一个工作流实例，并将该本地服务添加到工作流引擎中去。

我们开发一个简单的本地服务的例子，根据AccountID来修改Balance的值，并使用三种方式来调用：  
1.定义一个Account类，代码如下(Account.cs)

using System;  
namespace CaryWorkflows  
{  
    \[Serializable]  
    public class Account  
    {  
        private Int32 \_id;  
        private String \_name = String.Empty;  
        private Double \_balance;

        public Int32 Id  
        {  
            get { return \_id; }  
            set { \_id = value; }  
        }  
        public String Name  
        {  
            get { return \_name; }  
            set { \_name = value; }  
        }  
        public Double Balance  
        {  
            get { return \_balance; }  
            set { \_balance = value; }  
        }  
    }  
}  
2.定义一个接口，需要ExternalDataExchange属性，代码如下(IAccountServices.cs)：

```
using System;
using System.Workflow.Activities;
namespace CaryWorkflows 
{
    [ExternalDataExchange]
    public interface IAccountServices
    {
        Account AdjustBalance(Int32 id, Double adjustment);
    }
}
```

3.实现该接口，代码如下():

```
using System;
using System.Collections.Generic;
namespace CaryWorkflows 
{
    public class AccountService : IAccountServices
    {
        private Dictionary<Int32, Account> _accounts= new Dictionary<int, Account>();
        public AccountService()
        {
            Account account = new Account();
            account.Id = 101;
            account.Name = "Neil Armstrong";
            account.Balance = 100.00;
            _accounts.Add(account.Id, account);
        }
       public Account AdjustBalance(Int32 id, Double adjustment)
        {
            Account account = null;
            if (_accounts.ContainsKey(id))
            {
                account = _accounts[id];
                account.Balance += adjustment;
            }
            return account;
        }       
    }
}
```

```
 
```

```
服务定义好了，我们下面就要在工作流中条用该服务，我们有三种方式：
```

```

代码方式
```

```

在工作流中定义三个属性:
```

```
using System;
using System.Workflow.Activities;
namespace CaryWorkflows 
{
    public sealed partial class BalanceAdjustmentWorkflow: SequentialWorkflowActivity
    {
        private Int32 _id;
        private Double _adjustment;
        private Account _account;
        private IAccountServices _accountServices;

        public Int32 Id
        {
            get { return _id; }
            set { _id = value; }
        }
        public Double Adjustment
        {
            get { return _adjustment; }
            set { _adjustment = value; }
        }
        public Account Account
        {
            get { return _account; }
            set { _account = value; }
        }
        public BalanceAdjustmentWorkflow()
        {
            InitializeComponent();
        }
    }
}
```

```
 
```

```
然后我们向工作流中拖入一个CodeActivity，Activity有一个方法OnActivityExecutionContextLoad(),我们通过该
的IServiceProvider的GetService方法来获取本地服务，代码如下：
```

```
protected override void OnActivityExecutionContextLoad( IServiceProvider provider)
{
     base.OnActivityExecutionContextLoad(provider);           
     _accountServices = provider.GetService(typeof(IAccountServices))as IAccountServices;
     if (_accountServices == null)
     {                
        throw new InvalidOperationException("Unable to retrieve IAccountServices from runtime");
     }
}
```

```
 
```

```
在CodeActivity的ExecuteCode事件中调用本地服务的方法，代码如下：
```

```
 
```

```
private void codeAdjustAccount_ExecuteCode(object sender, EventArgs e)
{
     Account = _accountServices.AdjustBalance(Id, Adjustment);
}
```

```
 
```

```
最后要将该服务添加到工作流引擎当中去，
```

```
1. 先将ExternalDataExchangeService服务对象添加到引擎。
2.再将我们自己开发的服务绑定到ExternalDataExchangeService服务中。
```

```
宿主程序的代码如下：
```

```
using System;
using System.Collections.Generic;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using CaryWorkflows ;
namespace ConsoleLocalServices
{
    public class LocalServiceTest
    {
        public static void Run()
        {
            using (WorkflowRuntimeManager manager= new WorkflowRuntimeManager(new WorkflowRuntime()))
            {
                AddServices(manager.WorkflowRuntime);
                manager.WorkflowRuntime.StartRuntime();                
                Dictionary<String, Object> wfArguments= new Dictionary<string, object>();              
                Console.WriteLine("开始....");
                wfArguments.Add("Id", 101);
                wfArguments.Add("Adjustment", -25.00);
                WorkflowInstanceWrapper instance = manager.StartWorkflow(
typeof(CaryWorkflows.BalanceAdjustmentWorkflow), wfArguments);
                manager.WaitAll(2000);
                Account account = instance.OutputParameters["Account"] as Account;
                if (account != null)
                {
                    Console.WriteLine( "Revised Account: {0}, Name={1}, Bal={2:C}",account.Id,
                                                                 account.Name, account.Balance);
                }
                else
                {
                    Console.WriteLine("Invalid Account Id\n\r");
                }

                Console.WriteLine("结束....");
            }
        }        
        private static void AddServices(WorkflowRuntime instance)
        {
            ExternalDataExchangeService exchangeService = new ExternalDataExchangeService();
            instance.AddService(exchangeService);            
            exchangeService.AddService(new AccountService());
        }
    }
}
```

```
这样我们使用代码方式调用外部方法就结束了，结果如下：
开始....
Revised Account: 101, Name=Neil Armstrong, Bal=￥75.00
结束....
```

```
 
```

```
配置文件方式
```

```
 
```

```
1.添加一个app.config到项目中，代码如下：
```

```
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="WorkflowRuntime" 
      type="System.Workflow.Runtime.Configuration.WorkflowRuntimeSection,
        System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral,
        PublicKeyToken=31bf3856ad364e35" />
    <section name="LocalServices" 
      type="System.Workflow.Activities.ExternalDataExchangeServiceSection, 
        System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, 
        PublicKeyToken=31bf3856ad364e35"/>
  </configSections>
  <WorkflowRuntime Name="ConsoleLocalServices" >
    <CommonParameters>
      <!--Add parameters common to all services-->
    </CommonParameters>
    <Services>
      <!--Add core services here-->
    </Services>
  </WorkflowRuntime>
  <LocalServices >
    <Services>
      <!--Add local services here-->
      <add type="CaryWorkflows.AccountService, 
        CaryWorkflows,Version=1.0.0.0, 
        Culture=neutral, PublicKeyToken=null" />
    </Services>
  </LocalServices >
</configuration>
```

2.我们只要需改动宿主程序中如下部分：

```
using (WorkflowRuntimeManager manager= new WorkflowRuntimeManager(new 
                                                           WorkflowRuntime("WorkflowRuntime")));
ExternalDataExchangeService exchangeService = new ExternalDataExchangeService("LocalServices");
```

```
 
```

```
使用自定义活动方式
```

```

1.首先自定义一个活动(AdjustAccountActivity.cs), 我们在自定义活动中获取本地服务，并且调用其中方法，代码如下：
```

```
using System;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Workflow.Activities;
namespace CaryWorkflows 
{
    public partial class AdjustAccountActivity : Activity
    {
        public static DependencyProperty IdProperty= System.Workflow.ComponentModel
               .DependencyProperty.Register("Id", typeof(Int32), typeof(AdjustAccountActivity));
        [Description("Identifies the account")]
        [Category("Local Services")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Int32 Id
        {
            get
            {
                return ((Int32)(base.GetValue(AdjustAccountActivity.IdProperty)));
            }
            set
            {
                base.SetValue(AdjustAccountActivity.IdProperty, value);
            }
        }

        public static DependencyProperty AdjustmentProperty = System.Workflow.ComponentModel.
        DependencyProperty.Register("Adjustment", typeof(Double), typeof(AdjustAccountActivity));
        [Description("The adjustment amount")]
        [Category("Local Services")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Double Adjustment
        {
            get
            {
                return ((Double)(base.GetValue(AdjustAccountActivity.AdjustmentProperty)));
            }
            set
            {
                base.SetValue(AdjustAccountActivity.AdjustmentProperty, value);
            }
        }

        public static DependencyProperty AccountProperty= System.Workflow.ComponentModel.
          DependencyProperty.Register("Account", typeof(Account), typeof(AdjustAccountActivity));
        [Description("The revised Account object")]
        [Category("Local Services")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Account Account
        {
            get
            {
                return ((Account)(base.GetValue( AdjustAccountActivity.AccountProperty)));
            }
            set
            {
                base.SetValue(AdjustAccountActivity.AccountProperty, value);
            }
        }

        public AdjustAccountActivity()
        {
            InitializeComponent();
        }

        protected override ActivityExecutionStatus Execute( ActivityExecutionContext 
                                                                               executionContext)
        {
            IAccountServices accountServices =executionContext.GetService<IAccountServices>();
            if (accountServices == null)
            {
                throw new InvalidOperationException( "fail IAccountServices from runtime");
            }
            Account = accountServices.AdjustBalance(Id, Adjustment);
            return base.Execute(executionContext);
        }
    }
}
```

```
2.在工作流中我们将该自定义活动拖到工作流中，并设置相应的属性即可。
```

```
 
```

```
使用CallExternalMethodActivity
```

```
 
```

```
使用该方式我们只需要拖一个CallExternalMethodActivity到工作流中，并且设置起相应属性即可，如下图：
 
```

```
这三种方式的执行结果都是一样的。
```

```
上一篇：坚持学习WF(7):流程控制(Flow Control)
下一篇：坚持学习WF(9):本地服务之事件处理 
```