1

Using BizTalk 2013r2 CU1, I have a created a property schema for my inbound xsd and deployed the application.

When I receive a sample xml document using a standard "xml receive" pipeline then I can see that the required element is promoted into the context as expected.

I then created a custom pipeline which contains the "XML disassembler" component in the "Disassemble" stage and a custom component in the "Validate" stage. This custom component needs to read the promoted property from the context. However, I find that when I switch the Receive Location from "xml receive" pipeline to my custom pipeline then my property does not get promoted. I am using the following code within my custom component to write out a list of items in the message context:

for (int x = 0; x < contextList.CountProperties; x++)
        {
            contextList.ReadAt(x, out name, out nspace);
            string value = contextList.Read(name, nspace).ToString();
            contextItems += "Name: " + name + " - " + "Namespace: " + nspace + " - " + value + "\r\n";
            if (name == _ContextPropertyName && nspace == _ContextPropertyNamespace)
                promotedPropFound = true;

        }
        Helpers.EventLogHelper eventHelper = new EventLogHelper();
        eventHelper.LogEvent(string.Format("Context items:{0}", contextItems));

        if (promotedPropFound == false)
            throw new Exception(string.Format("Unable to find promoted property with name[{0}] and namespace [{1}]", _ContextPropertyName, _ContextPropertyNamespace));

From the output in the event log I can see that certain properties such as MessageType have been promoted but my custom property has not. Again, if I change the receive location back to use a standard "xml receive" pipeline then the property will be promoted from a copy of the same xml document (I check this by stopping the subscribing send port and viewing the context from the admin console).

I find this very strange since the same "XML disassembler" component is present in the same "Disassemble" stage of both pipelines, with the same (default)configuration. I'm starting to think perhaps there's a problem with 2013r2CU1 - has anyone else encountered the same?

2 Answers 2

7

By the time the XML Disassembler has executed in your custom pipeline, there is no guarantee that your properties have been promoted.

The incoming message arrives in the pipeline as a stream with the data pointer set at the start of the stream.
I think the XML Disassembler does not read the stream, it wraps it into some stream wrapper class that will populate the promoted properties when the stream actually gets read.
The stream will have to be read at least once: when the message gets inserted into the message box. So there is a guarantee that the properties will get promoted, but you cannot assume it will be done before the "Validate" stage executes.

To make sure this is really the problem your are encountering: check your message AFTER it has been imported into the message box.
If your promoted property is there, what I described is probably what is happening.

Solutions:

To make your custom pipeline component work, the best solution would be to do just as the XML Disassembler: get the incoming stream and wrap it into a stream wrapper class that can trigger whatever functionality you need.

The assembly Microsoft.BizTalk.Streaming.dll has some wrapper class that might interest you: ForwardOnlyEventingReadStream.
This class has an event AfterLastReadEvent. You can create some EventHandler and have it subscribe to this event to trigger your custom functionality only after the stream has been fully read., and all properties have been promoted.

Your custom component would look like that:

public IBaseMessage Execute(IPipelineContext context, IBaseMessage message)
{
    Stream stream = message.BodyPart.GetOriginalDataStream();
    CForwardOnlyEventingReadStream eventingReadStream = new CForwardOnlyEventingReadStream(stream);
    eventingReadStream.AfterLastReadEvent += new AfterLastReadEventHandler(DoSomething);

    message.BodyPart.Data = eventingReadStream; 
    return message;
}

private static void DoSomething(object src, EventArgs args)
{
}

A less efficient way to solve your problem would be to read the stream fully in your custom component at the "Validate" stage and put the stream pointer back to the start of the stream.

Microsoft has some guidelines for when you're manipulating the message stream in pipeline component: https://msdn.microsoft.com/en-us/library/aa577699.aspx

Update:

OP needs to pass the message context to the Event Handler. It is possible using a Lambda expression:

public IBaseMessage Execute(IPipelineContext context, IBaseMessage message)
{
    Stream stream = message.BodyPart.GetOriginalDataStream();
    CForwardOnlyEventingReadStream eventingReadStream = new CForwardOnlyEventingReadStream(stream);
    eventingReadStream.AfterLastReadEvent += new AfterLastReadEventHandler((src, args) => DoSomething(src, args, message.Context));

    message.BodyPart.Data = eventingReadStream; 
    return message;
}

private static void DoSomething(object src, EventArgs args, IBaseMessageContext messageContext)
{
}

This SO question can be interesting for reference for passing the additional parameter: Pass parameter to EventHandler

5
  • Thanks for your detailed response Gary, I think you may be right. My problem now is how to wire this up. As far as I can see, the AfterLastReadEvent delegate doesn't accept the pipeline context - which I need to be able to access. I don't have a lot of experience with delegates - do you know if it's possible pass the the pipeline context when the event is raised?
    – Rob Bowman
    Jan 4, 2016 at 9:56
  • @RobBowman Yes it possible. I have updated my answer for that. I assume you want to pass the message context rather than the pipeline context.
    – user4084148
    Jan 4, 2016 at 23:25
  • Thanks for the update Gary. Sorry this is taking me so long but currently I only have 20 mins each morning to work on this. Do you know of a link to an example use of an EventingReadStream? I haven't been able to find one. In my "DoSomething" event I now have the code which executes the required map. My problem now is I don't know how to assign that back to the stream. Should I pass the stream as a ref parameter to the event, then any update to it will make it's way into BizTalk?
    – Rob Bowman
    Jan 6, 2016 at 8:19
  • @RobBowman Hi Rob. The only example I have is in my own snippets library with method DoSomething writing "LALA" to a file on the disk... Don't think it'll help you :) Regarding a map or modifying the message: You didn't mention it before, it confuses me as to what you are trying to do and how to help you. Probably worth creating a new SO questions with all details.
    – user4084148
    Jan 6, 2016 at 23:45
  • Hi Gary, the component does the following: 1. Identifies a element in the msg body which indicates which map should be used. 2. Calls the BRE passing the value of this element. 3. The BRE returns the strong name of the map. 4 The component executes the map and published the transformed xml to the msgbox. Because of my problem with point 1 and identifying the promoted context properties, I am currently using an XML reader to find the required element. It is point 1 I would like to change so that a promoted property can be used
    – Rob Bowman
    Jan 7, 2016 at 7:26
2

Can you do whatever you had planned for the Validate Stage in an Orchestration? That would be much easier.

If not, the most common solution to this specific problem is an intermediate Pipeline Component that forces a full read on the stream, though technically, you'd only have to read until the Promoted node is hit.

2
  • Yes, I may have to resort to breaking the stream of the pipeline if I can't make it work with the eventing read stream but I haven't given up yet :)
    – Rob Bowman
    Jan 6, 2016 at 16:46
  • 1
    Maybe just FYI, but the Xml Disassembler essentially uses events to write the properties itself. Your problem is that the relevant event will not fire until the element is read. So, if you're trying an event model, you risk getting stuck in the same place as xmldasm.
    – DTRT
    Jan 6, 2016 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.