NServiceBus – Internals message headers in MSMQ

NServiceBus Version: 3.3.8

Recently I ended up investigating how NServiceBus attaches headers to MSMQ messages. I’m recording my learning here as I found it difficult to track this information down on the web.

NServiceBus uses the Message.Extension property to attach a serialised Dictionary(<string>, <string) it uses the built in .net Xml Serialiser to serialise the object.

An example of the header XML :



  
		NServiceBus.Version
		3.3.8
	
	
		NServiceBus.TimeSent
		2013-07-04 15:40:14:019635 Z
	
	
		NServiceBus.RelatedTo
		7dc42ed0-7f95-4372-ba23-eea903c405816798665
	
	
		NServiceBus.EnclosedMessageTypes
		MyLib.Ns.MessageType, My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
	
	
		NServiceBus.OriginatingSagaId
		e96308e0-e2f1-48ae-916a-a1f10112b8ef
	
	
		NServiceBus.OriginatingSagaType
		MyLib.ns.Foo, My.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
	
	
		WinIdName
		Domjon.smith
	
	
		CorrId
	

This function shows how NSB serialises the dictionary to XML using the frameworks Xml serialiser.

 private static byte[] SerialiseMessageHeaders(Dictionary headers)
        {
            using (var stream = new MemoryStream())
            {
                headerSerializer.Serialize(stream, headers.Select(pair => new HeaderInfo { Key = pair.Key, Value = pair.Value }).ToList());
                return stream.ToArray();
            }
        }