根据微软的示例,以下是如何通过WCF流式传输文件的步骤:
现在,当传输完成时,我如何知道谁负责释放FileStream?
编辑:如果将代码放入“using”块中,则在客户端接收任何内容之前,流将被关闭。
// Service class which implements the service contract
public class StreamingService : IStreamingSample
{
public System.IO.Stream GetStream(string data)
{
//this file path assumes the image is in
// the Service folder and the service is executing
// in service/bin
string filePath = Path.Combine(
System.Environment.CurrentDirectory,
".\\image.jpg");
//open the file, this could throw an exception
//(e.g. if the file is not found)
//having includeExceptionDetailInFaults="True" in config
// would cause this exception to be returned to the client
try
{
FileStream imageFile = File.OpenRead(filePath);
return imageFile;
}
catch (IOException ex)
{
Console.WriteLine(
String.Format("An exception was thrown while trying to open file {0}", filePath));
Console.WriteLine("Exception is: ");
Console.WriteLine(ex.ToString());
throw ex;
}
}
...
现在,当传输完成时,我如何知道谁负责释放FileStream?
编辑:如果将代码放入“using”块中,则在客户端接收任何内容之前,流将被关闭。