在 BizTalk 管道组件中处理流?

发布于 2024-09-11 07:46:20 字数 869 浏览 6 评论 0原文

我对 BizTalk 和创建自定义管道组件相当陌生。我在示例中看到了类似于以下内容的代码:

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;
}

但该方法中没有任何地方会关闭或处置 StreamReader。该方法简单地退出。

通常,在使用 StreamReader 和其他类时,最佳实践是使用 using 语句,以便自动释放流。

是否有特殊原因(可能在 BizTalk 中)导致您不处置此 StreamReader?

我还没有找到任何关于这一点的信息。有人可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2024-09-18 07:46:20

一般来说,是的,关闭不再需要的读者和流是一个很好的做法。也就是说,不一定每次都 100% 需要。例如,关闭读取器会正常关闭底层流,但很可能,其他东西可能已经意识到该流,并会在正确的时间自行关闭它。

然而,好的做法是将在管道组件中使用的任何流(其生命周期与消息的生命周期相匹配)添加到 资源跟踪器,以便 BizTalk 可以在管道执行完成且消息已处理时自动处理它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文