Jump to content

BizTalk - how to create message on orchestration programically

Posted on:September 30, 2016 at 02:00 AM

Since 2 years I have the opportunity to work with BizTalk. This post is one of “BizTalk series”. I’ll keep posting interesting tricks that are easy to do, but not so easy to find on internet.

Today I’ll show you how to create a message inside BizTalk orchestration. There are several ways to assign messages in BizTalk orchestration. Although (as far as I know) message construction can be done only in “Construct Message” shape. Like so:

So here is the meat :)

public class MessageHelper
{
    public static XmlDocument CreateErrorMessage()
    {
        Response resp = new Response { Status = OperationStatus.Error };
        StringBuilder builder = new StringBuilder();
        DataContractSerializer serializer = new DataContractSerializer(typeof(Response));

        using (XmlWriter xmlWriter = XmlWriter.Create(builder))
        {
            serializer.WriteObject(xmlWriter, resp);
            xmlWriter.Flush();
        }

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(builder.ToString());

        return doc;
    }
}

Message constructed

[DataContract(Name = "Response", Namespace = "http://schemas.datacontract.org/2004/07/Project.SampleNamespace")]
public class Response
{
    public Response();

    [DataMember]
    public object Data { get; set; }

    [DataMember]
    public OperationStatus Status { get; set; }
}

As you can see the the DataContract and DataMember attributes are used by DataContractSerializer to create correct XML. Including namespaces :)

Of course Message_1 has the same schema type. Schema is generated based on the class for example.