So I was answering a question on a internal (to Microsoft) email list I am lucky to
be on.  Someone asked a question about some code they were having trouble with 
– the code in question was being called from an Orchestration in BizTalk and they
were passing in the message as XmlDocument – converting that to an XmlValidatingReader
as a way of validating the document against their schema.

I pointed them to my
post on using XLANGMessage instead of XmlDocument
for performance.  This
actually seems to have solved their problem (even though I didn’t think it would –
I was just sort of morally opposed to creating an XmlDocument and then turning it
into an XmlReader when they could just go directly to an XmlReader – why do two steps
when one step would work?).

Tim Wieman (one of the BizTalk Rangers) pointed out to me that my code didn’t explain
something really important that my earlier post doesn’t mention (perhaps I should
edit that post as well now that I am writing this).  If you look at the docs
for XLANGMessage it
is pretty clear that when you are passing XLANGMessage to a .NET component – and you
just plan on reading it during your method call – you *should* call Dispose on it
before the end of your call.  This means you should always put the usage of XLANGMessage
inside of a try block and call Dispose in a finally block (as the above documentation
link clearly shows).  This was my bad – because of course I am generally thinking
about .NET and not about COM.  Well – BizTalk isn’t COM based – but XLANGMessage
is reference counted. 

After a discussion with Tim and Paul Ringseth (one of the orignal developers of XLANGs)
I came to accept this implementation.  Why did I reject it at first (I think
on the email thread I called it “stupid” ;-)?  Well – it just felt unnatural
to require *me* as the callee (the method implementor) be responsible for cleaning
up the XLANGMessage object passed in by the caller.

In most APIs – if you were passed an object and then called Dispose on it – you’d
be breaking the rules of your interaction with that API (one person I ran this by
said “I’d be really pissed if you Disposed of *my* object” – that would be Mark Taparauskas
– another really smart guy who doesn’t blog ;-)).

As Paul explained – the message doesn’t really belong to the .NET runtime – it belongs
to the MessageBox database – so it is similar to a COM interop scenario.  The
interesting thing is that the system is ref counted by the Orchestration instance
in this case – so if you fail to call dispose – the reference gets cleaned up at the
end of the Orchestration.  So depending on how long your Orchestration runs for
(if it is short) it isn’t a big deal.

But still –  the general rule of thumb is – if you are only using an XLANGMessage
instance during a method call – make sure to call Dispose on it during that method
call.

Brings
me back to the days when we used to have heated discusions about Refernce Couting
versus GC.  I guess those days are not completely gone yet – even in a .NET
product like BizTalk.

 



Check out my BizTalk
R2 Training.