0

我正在尝试开发用于配置动态端口的通用 BizTalk 应用程序。我有一个编排,可以拉回每个端口的所有配置设置,我想遍历这些设置并配置端口。这些设置保存在 MSSQL 中,例如,其中两个属性是 PortName 和 Address。因此,在编排中,我想通过字符串变量 PortName 引用端口。那么是否有某种方法可以获取编排中所有端口的集合或通过字符串变量引用端口,即Port['MyPortName'](Microsoft.XLANGs.BaseTypes.Address) = "file://c:\test\out\%MessageId%.xml"谢谢

4

2 回答 2

2

为了从编排中动态配置动态逻辑发送端口,必须将设置存储到持久数据存储(例如数据库或配置文件)中,并实现一种在运行时动态分配这些属性的方法。

但首先,我们需要了解配置动态发送端口时发生了什么。

如何配置动态逻辑发送端口

从编排中配置动态逻辑发送端口的属性涉及两个步骤:

  • 首先,必须在发送端口上指定TransportType和目标地址属性。这通常在Expression Shape中完成,代码类似于:

    DynamicSendPort(Microsoft.XLANGs.BaseTypes.TransportType) = "文件"; DynamicSendPort(Microsoft.XLANGs.BaseTypes.Address) = "C:\Temp\Folder\%SourceFileName%";

  • 其次,必须在传出消息本身的上下文中指定任何其他传输属性。几乎所有 BizTalk 适配器都有其他属性,用于消息引擎和 XLANG/s 编排引擎之间的通信。例如,ReceivedFileName上下文属性用于动态设置特定名称,以便 FILE 适配器何时将传出消息保存在其目标位置。作为构造传出消息的一部分,最好在Assignment Shape内执行此操作:

    OutgoingMessage(FILE.ReceiveFileName) = "HardCodedFileName.xml"

您会注意到大多数配置属性必须在传出消息的上下文中指定,指定名称空间前缀(例如 FILE)、属性名称(例如 ReceiveFileName),显然,还需要指定分配给相应属性的值。

事实上,所有上下文属性都是存在于众所周知的 Microsoft.BizTalk.GlobalPropertySchemas.dll程序集中的类。这可以通过在 Visual Studio 的对象资源管理器中查找此程序集来确认。

Microsoft.BizTalk.GlobalPropertySchemas.dll 中的 FILE.ReceivedFileName

尽管配置动态逻辑发送端口所需的大多数上下文属性都存在于此特定程序集中,但并非所有属性都存在。例如,MSMQ BizTalk 适配器使用单独的程序集来存储其上下文属性。显然,第三方或自定义适配器也带有附加程序集。

因此,为了使用如下所述的灵活方法在动态发送端口上设置上下文属性,需要四个信息:

  • 包含上下文属性类的程序集的完全限定名称。
  • 命名空间前缀。
  • 属性名称。
  • 财产价值。

在持久性介质中存储端口设置

以下 .XSD 架构说明了一种用于序列化端口设置的可能结构。

ContextProperties XML 模式定义

一旦序列化,指定的上下文属性就可以很容易地存储在 SQL 数据库或配置文件中。例如,以下是本文中用作示例的设置:

ContextProperties 设置示例

配置动态逻辑发送端口的灵活方法

使用简单的帮助程序库,设置动态端口配置非常容易。首先,您必须从持久性介质中检索序列化设置。这可以使用 WCF-SQL 适配器和一个简单的存储过程轻松实现。

一旦检索到这些属性,就可以将这些属性反序列化为强类型的 C# 对象图。为此,首先使用以下命令行实用程序创建上面显示的 ContextProperties 模式的 C# 表示:

xsd.exe /classes /language:cs /namespace:Helper.Schemas .\ContextProperties.xsd

这会生成一个部分类,可以使用以下方法进行改进:

namespace Helper.Schemas
{
    public partial class ContextProperties
    {
        public static ContextProperties Deserialize(string text)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                byte[] buffer = Encoding.UTF8.GetBytes(text);
                stream.Write(buffer, 0, buffer.Length);
                stream.Seek(0, SeekOrigin.Begin);
                return (ContextProperties) 
                    Deserialize(
                          stream
                        , typeof(ContextProperties));
            }
        }

        public static Object Deserialize(Stream stream, Type type)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(type);
            return xmlSerializer.Deserialize(stream);
        }
    }
}

其次,应用此配置涉及根据反序列化的 ContextProperties 对象图中指定的上下文属性类的描述,从代码创建 XLANG/s 消息并使用反射动态设置上下文属性。

为此,我使用了从Paolo Salvatori的关于动态转换的系列文章中借用的技术,该技术包括创建自定义BTXMessage派生类,由 BizTalk XLANG/s 引擎在内部使用。

namespace Helper.Schemas
{
    using Microsoft.BizTalk.XLANGs.BTXEngine; // Found in Microsoft.XLANGs.BizTalk.Engine
    using Microsoft.XLANGs.Core; // Found in Microsoft.XLANGs.Engine

    [Serializable]
    public sealed class CustomBTXMessage : BTXMessage
    {
        public CustomBTXMessage(string messageName, Context context)
            : base(messageName, context)
        {
            context.RefMessage(this);
        }

        public void SetContextProperty(string assembly, string ns, string name, object value)
        {
            if (String.IsNullOrEmpty(ns))
                ns = "Microsoft.XLANGs.BaseTypes";
            if (String.IsNullOrEmpty(assembly))
                assembly = "Microsoft.BizTalk.GlobalPropertySchemas";

            StringBuilder assemblyQualifiedName = new StringBuilder();
            assemblyQualifiedName.AppendFormat("{0}.{1}, {2}", ns, name, assembly);

            Type type = Type.GetType(assemblyQualifiedName.ToString(), true, true);
            SetContextProperty(type, value);
        }

        internal void SetContextProperty(string property, object value)
        {
            int index = property.IndexOf('.');
            if (index != -1)
                SetContextProperty(String.Empty, property.Substring(0, index), property.Substring(index + 1), value);
            else
                SetContextProperty(String.Empty, String.Empty, property, value);
        }

    }
}

现在,难题的最后一块是如何在 Orchestration 中使用这个自定义类。这可以使用以下帮助代码在分配形状中轻松完成:

namespace Helper.Schemas
{
    using Microsoft.XLANGs.BaseTypes;
    using Microsoft.XLANGs.Core; // Found in Microsoft.XLANGs.Engine

    public static class Message
    {
        public static XLANGMessage SetContext(XLANGMessage message, ContextProperties properties)
        {
            try
            {
                // create a new XLANGMessage

                CustomBTXMessage customBTXMessage = new CustomBTXMessage(message.Name, Service.RootService.XlangStore.OwningContext);

                // add parts of the original message to it

                for (int index = 0; index < message.Count; index++)
                    customBTXMessage.AddPart(message[index]);

                // set the specified context properties

                foreach (ContextPropertiesContextProperty property in properties.ContextProperty)
                    customBTXMessage.SetContextProperty(property.assembly, property.@namespace, property.name, property.Value);

                return customBTXMessage.GetMessageWrapperForUserCode();
            }

            finally
            {
                message.Dispose();
            }
        }
    }
}

您可以在分配形状中使用此静态方法,如下面所示的代码,其中OutboundMessage表示您要设置上下文的消息:

OutboundMessage = Helper.Schemas.Message.SetContext(OutboundMessage, contextProperties);
于 2012-07-04T19:54:16.883 回答
1

首先,您不应该尝试使用 Orchestration 进行这样的配置更改。从技术上讲,做您想做的事情是可行的,但作为一种实践,您不应该将业务流程与管理混为一谈。

执行此类操作的最佳方法是编写一些普通脚本或 PowerShell。

要回答您的问题,您可以从 ExplorerOM http://msdn.microsoft.com/en-us/library/microsoft.biztalk.explorerom.btsorchestration_members(v=bts.20)中的 BtsOrchestration 类中获取所需的数据

于 2012-05-22T06:16:51.650 回答