9

I'm using a Webjob with the Windows Azure Storage SDK. When a new item shows up in a Queue, a method in my class is invoked. According to the SDK docs, if I take a TextWriter as a parameter to my method, the SDK will provide me with a TextWriter that I can write to which will show up in the Webjob's logging infrastructure. This makes it pretty easy to diagnose issues and troubleshoot things.

public async static void ProcessQueueMessage([QueueTrigger("queueName")]MyModelType model, TextWriter logger)
{

    await logger.WriteLineAsync(string.Format("Processing Item {0}", model.SasUrl));

    // Some work here

    await logger.WriteLineAsync(string.Format("Done Processing Item {0}", model.SasUrl));
}

However, very frequently, within the body of my method, the TextWriter is being disposed of. I'm getting the following exception on the 2nd logger.WriteLineAsync:

System.ObjectDisposedException was unhandled by user code
  HResult=-2146232798
  Message=Cannot write to a closed TextWriter.
  Source=mscorlib
  ObjectName=""
  StackTrace:
       at System.IO.__Error.WriterClosed()
       at System.IO.StringWriter.Write(Char[] buffer, Int32 index, Int32 count)
       at System.IO.TextWriter.WriteLine(String value)
       at System.IO.TextWriter.SyncTextWriter.WriteLine(String value)
       at System.IO.TextWriter.SyncTextWriter.WriteLineAsync(String value)
       at NameOfProject.Program.<ProcessQueueMessage>d__8.MoveNext() in c:\Dev\Path\To\Program.cs:line 173
  InnerException:

I can't find others having this problem, so I can't imagine there is a bug in the SDK or webjobs infrastructure.

Is there a way to tell if the logger is disposed of ahead of the call?

Is there a way to create a new logger within my method that will participate in the WebJobs logging subsystems and UI?

Catch22
  • 3,261
  • 28
  • 34
Nate Jackson
  • 549
  • 4
  • 15
  • Nothing obvious to me, but my guess would be it's some kind of async issue. There's a variety of corner case bugs possible with async static void console applications. Could you try dropping async and seeing if it repros still? – Chris Anderson Jul 20 '15 at 00:43
  • Is there any particular reason for using async await inside the WebJob handler? According to my understanding your web job will be invoked in a different thread other than the main thread by SDK. So that main thread will never be disturbed. – Joy George Kunjikkuru Jul 20 '15 at 15:04

1 Answers1

30

That's because your method returns void. Try returning Task instead

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
  • 2
    Did the same mistake. I guess since I returned void the calling thread did not wait for the method to complete and directly disposed the text writer. Should be marked as a solution. – Martin Sep 13 '15 at 20:20
  • Thank you! Just spent two hours trying to figure out why my TextWriter was disposed. – peregrination Mar 23 '17 at 22:48
  • Any idea why I'd be seeing the same error even though I'm not using async? – Sean Mar 24 '17 at 06:18
  • 1
    it doesn't work for me. I changed the function to "public static async Task ...", but still there is the same problem. – Wizmann Jun 08 '17 at 12:42
  • @Wizmann Make sure you are "await" ing any async method calls in your processing code. – bleeeah Oct 26 '17 at 13:56