« Creating TPM Partners programmatically | Main | BizTalk WCF Loopback Binding »
Friday
May182012

BizTalk WCF Loopback Binding, writing to the context

Yesterday I posted an article about writing a Custom WCF Loopback Binding. I was having trouble getting the properties to be written to the context of the message in BizTalk. The code in question was this block

private Message CreateResponseMessage(Message requestMessage)
{
Message message = Message.CreateMessage(MessageVersion.Default, requestMessage.Headers.Action,
requestMessage.GetReaderAtBodyContents());

if (_connection.ConnectionFactory.Adapter.PreserveProperties)
{
message.Properties.CopyProperties(requestMessage.Properties);
}

return message;
}

I incorrectly assumed the WCF-Custom adapter would promote all properties in the Message.Properties collection. Akshat Sharma came to the rescue and pointed out that

All the ‘things’ that you want to promote using WCF message properties should be inside an IEnumerable of key value pairs: IEnumerable<KeyValuePair<XmlQualifiedName, object>> and this IEnumerable should be a property in your WCF message with name : "http://schemas.microsoft.com/BizTalk/2006/01/Adapters/WCF-properties/Promote" or “http://schemas.microsoft.com/BizTalk/2006/01/Adapters/WCF-properties/WriteToContext

So the request message has all the properties that were in the context and if I want to write them back I’ll have to define an IEnumerable of key value pairs. Something like this:

private Message CreateResponseMessage(Message requestMessage)
{
string propertiesToPromoteKey = "http://schemas.microsoft.com/BizTalk/2006/01/Adapters/WCF-properties/WriteToContext";

XmlReader reader = requestMessage.GetReaderAtBodyContents();
Message message = Message.CreateMessage(MessageVersion.Default, requestMessage.Headers.Action, reader);
List<KeyValuePair<XmlQualifiedName, object>> propertiesToPromote = new List<KeyValuePair<XmlQualifiedName, object>>();

if (_connection.ConnectionFactory.Adapter.PreserveProperties)
{
foreach (var property in requestMessage.Properties)
{
if(property.Key.Contains("#"))
{
var keySplit = property.Key.Split('#');
XmlQualifiedName qualifiedName = new XmlQualifiedName(keySplit[1], keySplit[0]);
propertiesToPromote.Add(new KeyValuePair<XmlQualifiedName, object>(qualifiedName, property.Value));
}

}

message.Properties.Add(propertiesToPromoteKey, propertiesToPromote);
}

return message;
}

It also turns out some of the properties in the request message are not known to BizTalk, for example http://schemas.microsoft.com/BizTalk/2003/system-properties#BizTalkMessageID. If you try and promote this property you’ll get this error:

The Messaging Engine failed while executing the inbound map for the response message coming from the destination URL "loopback://" with the Message Type http://MessageType . Details:"Loading property information list by namespace failed or property not found in the list. Verify that the schema is deployed properly. "

If on the other hand you try and write that property to the context (not promoted) you’ll just see this error. Note the absence of any helpful information.

The adapter failed to transmit message going to send port "SndLoopback" with URL "loopback://". It will be retransmitted after the retry interval specified for this Send Port. Details:"Exception of type 'Microsoft.BizTalk.Message.Interop.BTSException' was thrown.".

So basically you’ll want to exclude all properties in these namespaces.

http://schemas.microsoft.com/BizTalk/2006/01/Adapters/WCF-properties
http://schemas.microsoft.com/BizTalk/2003/messageagent-properties
http://schemas.microsoft.com/BizTalk/2003/messagetracking-properties
http://schemas.microsoft.com/BizTalk/2003/system-properties

Most of those properties BizTalk will deal with when the response is published. From system-properties I only really care about the Operation as that will be handy to preserve on a looped message especially if it goes out to another WCF send port. Most of the properties that are of value are going to be from adapters like FILE.ReceivedFileName or MQSeries.BizTalk_CorrelationID and they should work.

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>