jedgard
jedgard

Reputation: 868

Passing MemoryStream / Streams beween APIs

I have a Web API project that calls another API, the second API would return a Stream or MemoryStream while the receiving API gets the Stream and uses it to process / flush a file to the user.

The question is, should the MemoryStream or Stream populated on the second API be disposed, the problem is that if it's disposed then the Stream can't be used on the receiving end (because it will be disposed?), should it be disposed on the receiving end?.

I would like to understand a bit more about this and would appreciate any input on it.

Or should I send a byte array instead , the contents of the stream would generate a file with over 600 thousand records on a specific file format.

Upvotes: 0

Views: 507

Answers (1)

PhonicUK
PhonicUK

Reputation: 13874

You shouldn't dispose of streams that are given to you and you didn't create yourself. It's up to the caller to decide when they're finished using it. Chances are they're going to wrap the constructor up in a using statement anyway so you don't want to prematurely dispose of it.

A byte array would probably be a bad idea with that big a result set as you'll end up holding the entire thing in RAM whereas a Stream (can be as the name implies) streamed without reading the entire thing at once.

Upvotes: 1

Related Questions