Copy context in custom pipeline component

I developed a custom pipeline component to perform a mapping in a send pipeline. In the Execute() method I create and return a new message.

newBizTalkMessage = pContext.GetMessageFactory().CreateMessage();
newBizTalkMessage.AddPart(“body”, pContext.GetMessageFactory().CreateMessagePart(), true);
newBizTalkMessage.BodyPart.Data = virtualStream;
newBizTalkMessage.BodyPart.Data.Seek(0, SeekOrigin.Begin);

All good, but I received the following error:

A message received by adapter “FILE” on receive location “Unknown ” with URI “Unknown URL ” is suspended.
Error details: Exception of type ‘Microsoft.BizTalk.Message.Interop.BTSException’ was thrown.
MessageId: {F78EAABC-D7B7-463E-B5BD-A043DB51A222}
InstanceID: {770B7EC4-F8F2-48C2-B015-1DC44F794974}

The problem was that I didn’t copy the context of the original message by adding the below code:

newBizTalkMessage.Context = pInMsg.Context;

If you don’t add the context from the original message and you create a new message using pContext.GetMessageFactory().CreateMessage(), you will create a message with an empty context. What you miss in that case is the outbound URI and adapter to be used. Because the file adapter is the first adapter used, you get an error message related to the FILE adapter.

Remark: If you receive a Biztalk Routing Failure Report for “” in a receive pipeline, you probably have the same issue. If you add a line to add the context from the original message (see statement above), the problem is solved.