流在WCF返回值 - 谁作主呢? [英] Stream as a return value in WCF - who disposes it?

查看:207
本文介绍了流在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.

推荐答案

服务负责关闭流,除非您更改默认行为<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.operationbehaviorattribute.autodisposeparameters.aspx">it确实它会自动(与defalut值的行为总是使用)。如果设置 OperationBehavior.AutoDisposeParameters 你必须注册处理程序 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).

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

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