创建自定义路线消息传递服务
作为 Microsoft BizTalk ESB 工具包的一部分的行程框架支持使用类执行行程步骤,这些类实现执行路线消息传送服务的 IMessagingService 接口。 如果希望服务负责以下事项,则可以实现自定义消息传递服务:
行程中配置的自定义消息验证
行程中配置的自定义消息转换
消息的自定义处理
在所有这些情况下,实现的自定义行程服务充当侦听器,并由调度程序管道组件调用。
基于消息传送的自定义行程服务或消息服务都实现 IMessagingService 接口。 此接口公开 Name 和 SupportsDisassemble 属性以及 Execute 和 ShouldAdvanceStep 方法。
Name 属性是服务的名称,因为它将显示在行程中。 它必须与 Esb.config 文件中的行程服务配置中配置的名称匹配。
SupportsDisassemble 属性指示所创建的自定义消息传递服务是否支持反汇编和执行多个解析程序。
ShouldAdvanceStep 方法采用当前行程步骤和当前消息,并返回一个布尔值,该值指示调度程序是否应在服务执行后提前行程。 在几乎所有情况下,此方法都应返回 true。
Execute 方法对消息传递服务至关重要,它包含将在运行时执行的逻辑。 它采用管道上下文、消息、解析程序字符串和当前行程步骤;并返回更新的消息。
Execute 方法的引用实现可以在 ESB 的 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;
}
}
实现消息传送的自定义行程服务
使用派生自 IMessagingService 的类创建程序集;在 Execute 方法中,包括对消息进行修改所需的所有逻辑,或者 () 消息上下文。
在 Esb.config 文件的 itineraryServices 部分中为服务添加一个<条目,方法是添加 GUID> 作为 id 属性、服务名称作为 name 属性、类的完全限定名称作为类型属性、Messaging 作为范围属性以及允许的阶段 (例如, OnRampReceive、OnRampSend、OffRampSend、OffRampReceive、AllSend、AllReceive 或 All) 作为阶段属性。
在全局程序集缓存中注册新程序集。