How to Finish() without closing stream
Closed this issue · 2 comments
kkapuscinski commented
Hi,
I have problem/question with this library.
We have a scenario creating spreedsheet on MemoryStream
and after using Finish()
on ExcelSpreadsheetWriter
causes Stream
to Close. I would like to pass it to the response in WCF message but only way i found is to copy stream to new stream which causes 2x amount of memory for this operation.
I tried looking in to code and i found two (maybe) possible options:
- I found
if (outputStream.GetType().FullName == "System.Web.HttpResponseStream") outputStream = (Stream) new WriteStreamWrapper(outputStream);
so i thought it will resolve my problem but it also closes the stream. - I found that
PackageWriteStream
which usesZipArchive
which on closing causesDispose()
which in turn closes the stream. But there is an optionleaveOpen
- Write Custom implementation of
Stream
that would allow copying underlyingBuffer
andLength
to newStream
which won't allocate newBuffer
And now i'm thinking if i am doing something wrong or maybe i should propose some implementation
danielgindi commented
- The
MemoryStream
is essentially abyte[]
. So you could take it through.ToArray()
and write it to your response stream. - You could pass
Response.OutputStream
directly to the spreadsheet writer. - You could open a PR that adds a flag telling it to not close the underlying stream :)
kkapuscinski commented
Hi,
- as i wrote earlier this is not acceptable due to new allocation of memory and sometimes those files are around 50 mb, so it will be allocated on LOH which GC will slow down whole process.
- I'm not sure to what kind
Response
You are referring to. I have WCF not WebAPI or MVC soHttpContext
is not available here. - I Think this is probably best way, ill make PR soon. Are there any UnitTests ?