假设我有以下WCF实现:
public Stream Download(string path)
{
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
return stream;
}
谁负责处理返回值?毕竟,网络故障可能会发生,因此消费者可能无法处理它。
发布于 2011-06-26 19:02:04
服务负责关闭流,除非您更改默认行为it does it automatically (始终使用带Defalut值的行为)。如果将OperationBehavior.AutoDisposeParameters
设置为false
,则必须为OperationContext.OperationCompleted
注册处理程序,并按照here所述在处理程序中处理流。
客户端无法关闭流,因为客户端具有不同的流-您没有传递对流的引用或对文件处理程序的引用。在内部,文件内容被复制到传输,客户端在它自己的流实例中处理它(在那里他负责处理它)。
发布于 2011-10-20 05:58:04
如果您将流包装在MessageContract中(这样您就可以在报头中发送更多信息),请注意流不会被自动处理!正如attribute OperationBehavior.AutoDisposeParameters的名称所暗示的那样,WCF会自动处理输入/输出参数,因此您必须在MessageContract类上实现IDisposable并关闭那里的流。
发布于 2014-11-16 16:51:10
您可以在WCF中处理返回的流,如下所示
FileStream stream=null;
OperationContext clientContext = OperationContext.Current;
clientContext.OperationCompleted += (sender, args) =>
{
if (stream != null)
stream.Dispose();
};
stream = new FileStream(path, FileMode.Open, FileAccess.Read);
return stream;
https://stackoverflow.com/questions/6483320
复制相似问题