views:

32

answers:

1

I'm fairly new to BizTalk and creating a custom pipeline component. I have seen code in examples that are similar to the following:

public void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
{

    Stream originalDataStream = pInMsg.BodyPart.GetOriginalDataStream();
    StreamReader strReader = new StreamReader(originalDataStream);

    string strOriginalData = strReader.ReadToEnd();

    byte[] bufferOriginalMessage = new byte[strOriginalData.Length];
    bufferOriginalMessage = ASCIIEncoding.Default.GetBytes(strOriginalData);

    Stream ms = new MemoryStream();
    ms.Write(bufferOriginalMessage, 0, strOriginalD

    //other stuff here

    ms.Seek(0, SeekOrigin.Begin);
    pInMsg.BodyPart.Data = ms;
}

But nowhere in the method is the StreamReader being closed or disposed. The method simply exits.

Normally when using StreamReader and other classes, it is best practice to use a using statement so that the stream is automatically disposed.

Is there a particular reason (perhaps in BizTalk) why you wouldn't dispose this StreamReader?

I have not found any information on this point. Can anyone help?

A: 

In general, yes, it's a good practice to close readers and streams you don't need anymore. That said, there might not necessarily be 100% required everytime. For example, closing the reader would close the underlying stream normally, but chances are, something else is probably already aware of the stream and will close it at the right time on it's own.

What is good practice, however, is to add any streams you use in a pipeline component with a lifetime matching that of the message to the resource tracker, so that BizTalk can dispose them automatically when the pipeline execution finishes and the message has been processed.

tomasr
Thanks, that was what I was thinking, but I couldn't find any documentation to that effect. I would think that if closing the stream was detrimental, then the docs would have highlighted that!
Chris Dunaway