Stream 作为 WCF 中的返回值 - 谁处理它? [英] Stream as a return value in WCF - who disposes it?

查看:10
本文介绍了Stream 作为 WCF 中的返回值 - 谁处理它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下 WCF 实现:

Let's say I have the following WCF implementation:

public Stream Download(string path)
{
    FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
    return stream;
}

谁负责处理返回的值?毕竟,可能会发生网络故障,因此消费者可能无法对其进行处理.

Who's responsible for disposing the returned value? After all, a network failure might occur, hence the consumer might not be able to dispose it.

推荐答案

服务负责关闭流,除非您更改默认行为 它会自动执行(始终使用具有默认值的行为).如果您将 OperationBehavior.AutoDisposeParameters 设置为 false,您必须为 OperationContext.OperationCompleted 注册处理程序并按照 这里.

Service is responsible for closing stream and unless you change default behavior it does it automatically (the behavior with defalut values is always used). If you set OperationBehavior.AutoDisposeParameters to false you must register handler for OperationContext.OperationCompleted and dispose the stream in the handler as described here.

客户端无法关闭流,因为客户端有一个不同的流 - 您没有传递对流的引用或对文件处理程序的引用.在内部文件内容被复制到传输和客户端在它自己的流实例中处理它(他负责处理它).

Client cannot close the stream because client has a different one - you are not passing reference to your stream or reference to your file handler. Internally file content is copied to transport and client processes it in its own stream instance (where he is responsible for disposing it).

这篇关于Stream 作为 WCF 中的返回值 - 谁处理它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆