开发者

Dispose a stream in a BizTalk pipeline component?

开发者 https://www.devze.com 2023-01-07 22:20 出处:网络
I\'m fairly new to BizTalk and creating a custom pipeline component.I have seen code in examples that are similar to the following:

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消