我有一个简单的 WCF 服务,它公开一个 REST 端点,并从 BLOB 容器中获取文件。该服务将文件作为流返回。我偶然发现了这篇关于在做出响应后关闭流的帖子:
http://devdump.wordpress.com/2008/12/07/disposing-return-values/
这是我的代码:
public class FileService
{
[OperationContract]
[WebGet(UriTemplate = "{*url}")]
public Stream ServeHttpRequest(string url)
{
var fileDir = Path.GetDirectoryName(url);
var fileName = Path.GetFileName(url);
var blobName = Path.Combine(fileDir, fileName);
return getBlob(blobName);
}
private Stream getBlob(string blobName)
{
var account = CloudStorageAccount.FromConfigurationSetting("ConnectingString");
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("data");
var blob = container.GetBlobReference(blobName);
MemoryStream ms = new MemoryStream();
blob.DownloadToStream(ms);
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
}
所以我有两个问题:
- 我应该遵循帖子中提到的模式吗?
- 如果我将返回类型更改为 Byte[],Cons/Pros 是什么?
(我的客户端是Silverlight 4.0,以防万一有什么影响)