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

后端 未结 2 1749
清歌不尽
清歌不尽 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.

提交回复
热议问题