Friday 26 December 2014

String to XLangMessage in BizTalk

Create an XLangMessage from a string. Do keep in mind, that you have to pass in valid XML message text.

public static XLANGMessage CreateXLANGMessageFromString(string xmlMessage, string messagePart1Name, string messagePart2Name, string messagePart3Name)
{
    // Create a VirtualStream for memory optimization
    int bufferSize = 640;
    int thresholdSize = 1048576; // 1MB
    var stream = new VirtualStream(bufferSize, thresholdSize);
    // Make sure booleans are in lowercase
    xmlMessage = xmlMessage.Replace("False", "false").Replace("True", "true");
    // The name of the root node
    var rootNodeName = String.Empty;
    // Create a reader which will be used to read through the message
    using (var reader = new XmlTextReader(new StringReader(xmlMessage)))
    {
        // Create a writer used to write the outgoing message
        using (var writer = XmlWriter.Create(stream, new XmlWriterSettings { ConformanceLevel = ConformanceLevel.Auto, Encoding = Encoding.UTF8 }))
        {
            // Boolean indicating if the root node name has been set
            var rootNodeNameSet = false;

            // Read through the message
            while (reader.Read())
            {
                // Check if we need to set the root node name here
                if (!rootNodeNameSet && reader.NodeType == XmlNodeType.Element)
                {
                    // Set the root node name
                    rootNodeName = reader.LocalName;
                    rootNodeNameSet = true;
                }

                // Write the current element
                ProcessMessage.WriteShallowNode(reader, writer);
            }
        }
    }

    // Reset the stream
    stream.Seek(0, SeekOrigin.Begin);

    // Create a CustomBTXMessage, which will be used to create our XLANGMessage
    var customBTXMessage = new CustomBTXMessage(rootNodeName, Service.RootService.XlangStore.OwningContext);

    // Add message part
    customBTXMessage.AddPart(String.Empty, messagePart1Name);

    // Load the message we just created
    customBTXMessage[0].LoadFrom(stream);

    // Add extra message parts if needed
    if (!String.IsNullOrWhiteSpace(messagePart2Name))
    {
        // Add message part
        customBTXMessage.AddPart(String.Empty, messagePart2Name);
    }

    // Add extra message parts if needed
    if (!String.IsNullOrWhiteSpace(messagePart3Name))
    {
        // Add message part
        customBTXMessage.AddPart(String.Empty, messagePart3Name);
    }

    // Return the XLANGMessage
    return customBTXMessage.GetMessageWrapperForUserCode();
}

make sure you have the following using statements in your class:

using Microsoft.BizTalk.Streaming;
using Microsoft.XLANGs.BaseTypes;
using Microsoft.XLANGs.Core;

Custom BTX class

using Microsoft.BizTalk.XLANGs.BTXEngine;
using System;
using Microsoft.XLANGs.Core;

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

No comments:

Post a Comment