Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以流形式从WCF服务返回Azure BLOB-我们需要关闭它吗?_Wcf_Silverlight 4.0_Azure_Azure Storage_Azure Storage Blobs - Fatal编程技术网

以流形式从WCF服务返回Azure BLOB-我们需要关闭它吗?

以流形式从WCF服务返回Azure BLOB-我们需要关闭它吗?,wcf,silverlight-4.0,azure,azure-storage,azure-storage-blobs,Wcf,Silverlight 4.0,Azure,Azure Storage,Azure Storage Blobs,我有一个简单的WCF服务,它公开REST端点,并从BLOB容器中获取文件。该服务将文件作为流返回。在回复后,我无意中发现了这篇关于关闭流的帖子: 这是我的代码: 公共类文件服务 { [经营合同] [WebGet(UriTemplate=“{*url}”)] 公共流ServeHttpRequest(字符串url) { var fileDir=Path.GetDirectoryName(url); var fileName=Path.

我有一个简单的WCF服务,它公开REST端点,并从BLOB容器中获取文件。该服务将文件作为流返回。在回复后,我无意中发现了这篇关于关闭流的帖子:

这是我的代码:

公共类文件服务
{
[经营合同]
[WebGet(UriTemplate=“{*url}”)]
公共流ServeHttpRequest(字符串url)
{                                
var fileDir=Path.GetDirectoryName(url);
var fileName=Path.GetFileName(url);
var blobName=Path.Combine(fileDir,fileName);
返回getBlob(blobName);
}
私有流getBlob(字符串blobName)
{
var account=CloudStorageAccount.FromConfigurationSetting(“连接字符串”);
var client=account.CreateCloudBlobClient();
var container=client.GetContainerReference(“数据”);
var blob=container.GetBlobReference(blobName);
MemoryStream ms=新的MemoryStream();
blob.DownloadToStream(ms);
Seek女士(0,SeekOrigin.Begin);
返回ms;
}
}
所以我有两个问题:

  • 我应该遵循帖子中提到的模式吗
  • 如果我将返回类型更改为Byte[],则有哪些利弊

  • (我的客户是Silverlight 4,以防有任何影响)

    我会考虑将您的返回类型更改为<代码>字节[]/COD>。它更整洁

    Stream
    实现了
    IDisposable
    ,因此理论上,方法的使用者需要使用
    块调用
    中的代码:

    using (var receivedStream = new FileService().ServeHttpRequest(someUrl))
    {
       // do something with the stream
    }
    

    如果您的客户确实需要访问
    提供的内容,那么一定要继续并返回该内容,但要返回
    字节[]
    您可以控制隐藏在封面下的任何非托管资源。

    OperationBehaviorAttribute.AutoDisposeParameters默认设置为TRUE,对所有一次性输入/输出调用dispose。所以一切正常。
    此链接:

    说明如何手动控制该过程