创建自定义路线消息传送服务

属于 Microsoft BizTalk ESB 工具包的行程框架支持使用实现执行路线消息服务的 IMessagingService 接口的类执行行程步骤。 如果希望服务负责以下各项,则可以实现自定义消息传送服务:

  • 行程中配置的自定义消息验证

  • 行程中配置的自定义消息转换

  • 消息的自定义处理

    在所有这些情况下,你实现的自定义行程服务充当拦截器,并由调度程序管道组件调用。

    基于自定义消息传送的路线服务或消息传递服务均实现 IMessagingService 接口。 此接口公开 NameSupportsDisassemble 属性以及 ExecuteShouldAdvanceStep 方法。

    Name 属性是服务的名称,因为它将显示在行程中。 它必须与 Esb.config 文件中路线服务配置中的配置名称匹配。

    SupportsDisassemble 属性指示所创建的自定义消息传送服务是否支持反汇编和执行多个解析程序。

    ShouldAdvanceStep 方法采用当前行程步骤和当前消息,并返回一个布尔值,该值指示调度程序是否应在服务执行后推进行程。 几乎所有情况下,此方法都应返回 true

    Execute 方法对于消息传送服务非常重要,并且包含将在运行时执行的逻辑。 它接收管道上下文、消息、解析器字符串和当前行程步骤,然后返回更新的消息。

    Execute 方法的参考实现可以在 ESB.Itinerary.Services 项目的 RoutingService.cs 文件中找到,如以下代码所示。

public IBaseMessage ExecuteRoute(IPipelineContext context, IBaseMessage msg, string resolverString)  
        {  
            if (context == null)  
                throw new ArgumentNullException("context");  
            if (msg == null)  
                throw new ArgumentNullException("msg");  
            if (string.IsNullOrEmpty(resolverString))  
                throw new ArgumentException(Properties.Resources.ArgumentStringRequired, "resolverString");  
            try  
            {  
                ResolverInfo info = ResolverMgr.GetResolverInfo(ResolutionType.Endpoint, resolverString);  
                if (!info.Success)  
                    throw new RoutingException(Properties.Resources.ResolverStringInvalid, resolverString);  
  
                // Resolve configuration for routing.  
                Dictionary<string, string> resolverDictionary = ResolverMgr.Resolve(info, msg, context);  
  
                if (string.IsNullOrEmpty(resolverDictionary["Resolver.TransportLocation"]))  
                    throw new RoutingException(Properties.Resources.TransportLocationNotResolved, resolverString);  
  
                AdapterMgr.SetEndpoint(resolverDictionary, msg.Context);  
  
                return msg;  
            }  
            catch (System.Exception ex)  
            {  
                EventLogger.Write(MethodInfo.GetCurrentMethod(), ex);  
                throw;  
            }        
        }  

实现用于消息传递的自定义路线服务

  1. 使用派生自 IMessagingService 的类创建程序集;在 Execute 方法中,包括对消息或消息上下文进行修改所需的所有逻辑(如果有)。

  2. 通过在Esb.config文件的行程服务部分中添加一个<行程服务>元素,为您的服务设置入口,其中ID属性为GUID,服务名称为名称属性,类的完全限定名称为类型属性,消息范围属性,允许的阶段(例如,OnRampReceiveOnRampSendOffRampSendOffRampReceiveAllSendAllReceiveAll)为阶段属性。

  3. 在全局程序集缓存中注册新程序集。