Creating Multiple Messages in a BizTalk Server disassemble pipeline component

There is a interface IBaseMessageFactory that you can use to create instances of BizTalk messages and BizTalk message parts.

You can grab a handle to an instance of a subtype of this interface from within the Dissassemble method using the IPipelineContext.GetMessageFactory().

An important thing to note is that in order to preserve the message context you must grab this from the original message and poke it into the new instances created by the factory. The following code illustrates it.

 1public void Disassemble(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)  
 3    IBaseMessageContext sourceContext = inmsg.Context;  
 4    IBaseMessagePart part = inmsg.BodyPart;  
 6    // queue four messages to be processed in the GetNext() method.  
 7    // \_msgs is a simple Queue  
 8    \_msgs.Enqueue(CreateMessage(pc, sourceContext,part));  
 9    \_msgs.Enqueue(CreateMessage(pc, sourceContext,part));  
10    \_msgs.Enqueue(CreateMessage(pc, sourceContext,part));  
11    \_msgs.Enqueue(CreateMessage(pc, sourceContext,part));  
14private string messageType = @"";  
15private string systemPropertiesNamespace = @"";  
17public IBaseMessage CreateMessage(IPipelineContext pc, IBaseMessageContext sourceContext, IBaseMessagePart part)  
19    IBaseMessage msg = pc.GetMessageFactory().CreateMessage();  
20    msg.AddPart("Body", part, true);  
23    // comment out the following line to preserve the original message  
24    msg.BodyPart.Data = new MemoryStream(ASCIIEncoding.ASCII.GetBytes("blah blah blah"));  
25    msg.Context = sourceContext;  
26    msg.Context.Promote("MessageType", systemPropertiesNamespace, messageType);  
27    return msg;