I have a WCF service, from which users can request large datafiles (stored in an SQL database with FileStream enabled). These files should be streamed, and not loaded into m
Normally I might suggest wrapping the stream in a custom stream that closes the transaction when disposed, however IIRC WCF makes no guarantees about which threads do what, but TransactionScope
is thread-specific. As such, perhaps the better option is to copy the data into a MemoryStream
(if it isn't too big) and return that. The Stream.Copy
method in 4.0 should make that a breeze, but remember to rewind the memory-stream before the final return
(.Position = 0
).
Obviously this will be a big problem if the stream is big, ... but, if the stream is big enough for that to be a concern, then personally I'd be concerned at the fact that it is running in TransactionScope
at all, since that has inbuilt time limits, and causes serializable isolation (by default).
A final suggestion would be to use a SqlTransaction
, which is then not thread-dependent; you could write a Stream
wrapper that sits around the SqlFileStream
, and close the reader, transaction and connection (and the wrapped stream) in the Dispose()
. WCF will call that (via Close()
) after processing the results.