不需要Orchestration,通过Pipeline设定动态发送端口属性

 

不需要Orchestration,通过Pipeline设定动态发送端口属性

通常情况下使用动态发送端口,需要Orchestration中使用表达式(Expression)指定具体的发送端口目的地址Port_1(Microsoft.XLANGs.BaseTypes.Address) = http://www.tradingpartner.com/receive.ashx     具体的地址可以根据传入的Message通过xpath表达式或是Promote属性获取,但是每个流程只能接收制定的Schema消息,如果想做一个通用的根据消息路由就不是很方便了。

实际情况是这样,有100 不同的Schema,需要根据具体的消息实例的内容进行路由,具体的地址存放在“路由表”中,可以动态维护,一开始通过OrchestrationReciveMessage的类型定义为通用的XmlDocument处理,在流程中通过xpath获取值后再去找“路由表”中对应的地址,设定动态发送端口地址。

这样做很显然效率很差,而且不规范,容易冲突

现在可以直接通过自定义开发receivepipeline组件,在pipeline执行时把需要的地址通过属性升级(promote)方式赋值,这样动态发送端口就可以直接根据具体属性值进行发送到指定的目的地。

注意:动态发送端口只能订阅到具有promote OutboundTransportType 和OutboundTransportLocation 属性的消息,如果以上两个属性没有升级,只是通过ReceivePortName或其他属性是无法订阅到消息的。

代码:

IBaseMessageContext context = inmsg.Context;

            IBaseMessagePart bodyPart = inmsg.BodyPart;

            Stream originalStrm = bodyPart.GetOriginalDataStream();

 

            string inval = string.Empty;

            string outboundaddress = string.Empty;

            string[] fixstr;

            inval = Convert.ToString(context.Read(this._PropName, this._PropNamespaces));

            if (string.IsNullOrEmpty(inval))

            {

                //inval = GetInMessageValue(originalStrm);

            }

            //outboundaddress = this.GetTransportLocation(inval);

            outboundaddress = @"msmq://.\private$\mq1";

            if (outboundaddress.IndexOf("://") > 0)

            {

                fixstr = outboundaddress.Split(':');

                this._PropTransportType = fixstr[0];

                int firstidx = outboundaddress.IndexOf("//");

                outboundaddress = outboundaddress.Substring(firstidx + 2);

            }

            if (this._PropTransportType.ToLower().Trim() == "msmq")

            {

                context.Promote("Transactional", "http://schemas.microsoft.com/BizTalk/2003/msmq-properties", this._PropTransactional);

                context.Promote("TimeOut", "http://schemas.microsoft.com/BizTalk/2003/msmq-properties", 4);

                context.Promote("TimeOutUnits", "http://schemas.microsoft.com/BizTalk/2003/msmq-properties", "Days");

            }

            context.Promote("OutboundTransportType", "http://schemas.microsoft.com/BizTalk/2003/system-properties", this._PropTransportType);

            context.Promote("OutboundTransportLocation", "http://schemas.microsoft.com/BizTalk/2003/system-properties", outboundaddress);

            if (!string.IsNullOrEmpty(this._PropSPID.Trim()))

                context.Promote("SPID", "http://schemas.microsoft.com/BizTalk/2003/system-properties", this._PropSPID);

 

 

 

 

 

 

 

 

 

            bodyPart.Data = originalStrm;

            pc.ResourceTracker.AddResource(originalStrm);

 

            return inmsg;

这样做自然性能会提高很多,方便维护

原文地址:https://www.cnblogs.com/neozhu/p/1657100.html