0

I have a list of objects and I am trying to write a list of some of them to a .txt file and then upload to blob storage. The file is created and uploaded fine, but it does not contain any text. Here is my attempt:


        private async Task WriteChangedFileAsync(DataChange<StudentLookupData>[] changedResults, string containerName, string fileName)
        {
            var changesStream = new MemoryStream();
            var streamWriter = new StreamWriter(changesStream);

            foreach (DataChange<StudentLookupData> studentData in changedResults)
            {
                if (studentData.Change == ChangeType.Added)
                {
                    streamWriter.WriteLine("Added:");
                    streamWriter.WriteLine(studentData.Data.StudentCode);
                }
                else if (studentData .Change == ChangeType.Removed)
                {
                    streamWriter.WriteLine("Deleted:");
                    streamWriter.WriteLine(studentData .Data.StudentCode);
                }
            }
            await _fileStore.WriteFileAsync(new FileData()
            {
                Container = containerName,
                Name = fileName,
                ContentType = AppSettingsConstants.ContentTypes.Text,
                FileStream = changesStream
            }, true);
        }
    }

Can anyone please point out where I am going wrong here? Thank you!

Jordan1993
  • 864
  • 1
  • 10
  • 28
  • 5
    Rewind the stream; it's at its end and there's nothing more to read. – CodeCaster Sep 24 '20 at 13:37
  • 3
    And flush the writer by surrounding it in a using statement – Magnus Sep 24 '20 at 13:42
  • 2
    `streamWriter.Flush()` then `changesStream.Position = 0` – HMZ Sep 24 '20 at 13:54
  • @Magnus [no, this disposes the MemoryStream](https://stackoverflow.com/questions/1187700/does-disposing-a-streamwriter-close-the-underlying-stream). They need to flush before calling `_fileStore.WriteFileAsync()`, and dispose it afterwards. – CodeCaster Sep 24 '20 at 14:10
  • @CodeCaster You would still need to dispose of the writer and stream, right? – HMZ Sep 24 '20 at 14:36
  • @CodeCaster Just confirmed that the `StreamWriter` **Closes** the stream but doesn't dispose of it. [Source](https://referencesource.microsoft.com/#mscorlib/system/io/streamwriter.cs,f922405b11eca536,references). – HMZ Sep 24 '20 at 14:48
  • @HMZ of course they should dispose `changesStream` and `streamWriter`, but neither must be disposed nor closed before the call to `_fileStore.WriteFileAsync()` returns is what I meant. The StreamWriter does have to be flushed before the call, as you indicated as well. – CodeCaster Sep 24 '20 at 15:19
  • @CodeCaster In the ctor of the StreamWriter you can specify if it should close the underlying stream or not on dispose. – Magnus Sep 25 '20 at 09:24
  • @Magnus yes, but you said to wrap it in a using statement to flush it. That does not work here, that will flush too late. I was just responding to that. – CodeCaster Sep 25 '20 at 09:26
  • @CodeCaster You end the scope of the writer before WriteFileAsync and the scope of the stream after. – Magnus Sep 25 '20 at 09:27
  • @Magnus yes of course that's a solution, but that was not in your original comment, and IMHO that makes for weird nesting that makes the code less readable. I'd prefer the syntax as shown in the accepted answer of the duplicate. – CodeCaster Sep 25 '20 at 09:29
  • @CodeCaster I never said where to end the using block in my comment, but if you use it for flushing obviously it has to be before you send the data. – Magnus Sep 25 '20 at 09:31

0 Answers0