Returning Azure BLOB from WCF service as a Stream - Do we need to close it?

后端 未结 2 1747
清歌不尽
清歌不尽 2021-01-26 09:54

I have a simple WCF service that exposes a REST endpoint, and fetches files from a BLOB container. The service returns the file as a stream. i stumbled this post about closing t

相关标签:
2条回答
  • 2021-01-26 10:14

    I'd consider changing your return type to byte[]. It's tidier.

    Stream implements IDisposable, so in theory the consumer of your method will need to call your code in a using block:

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

    If your client definitely needs access to something that Stream provides, then by all means go ahead and return that, but by returning a byte[] you keep control of any unmanaged resources that are hidden under the covers.

    0 讨论(0)
  • 2021-01-26 10:17

    OperationBehaviorAttribute.AutoDisposeParameters is set to TRUE by default which calls dispose on all the inputs/outputs that are disposable. So everything just works.
    This link :
    http://devdump.wordpress.com/2008/12/07/disposing-return-values/
    explains how to manually control the process.

    0 讨论(0)
提交回复
热议问题