- Code: Select all
public class DocumentConverter
{
public Stream ConvertToPdf(Stream file)
{
var document = new Spire.Doc.Document();
var result = new MemoryStream();
document.LoadFromStream(file, FileFormat.Auto);
document.SaveToStream(result, FileFormat.PDF);
return result;
}
}
The WebJob monitors an Azure Storage Queue. If a large number of messages are sent to the queue at the same time (say, for example, 30 messages) the code above will often throw an exception at document.SaveToStream(result, FileFormat.PDF). The exception will either be a NullReferenceException, an InvalidOperationException (Collection was modified; enumeration operation may not execute), or an IndexOutOfRangeException. My understanding is that when a WebJob receives multiple messages from a queue it will process those messages in parallel.
It is worth noting that I am also converting Excel spreadsheets to PDFs in exactly the same way and that it works as expected.
We are using v5.5.36.4040 of Spire.Doc. Is this a bug with the Spire.Doc library?
I have included the stack trace of one of the exceptions below:
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Enumerator.MoveNext()
at spr3837.30(spr7394 A_0)
at spr3837.24(spr7394 A_0, Boolean A_1)
at spr3837.24(spr7394 A_0)
at spr347.24(spr6233 A_0, spr43 A_1)
at spr8247.2(spr6233 A_0, spr43 A_1, spr6456 A_2)
at spr8247.0(spr6233 A_0, spr6456 A_1)
at spr8247.5(spr6456 A_0)
at spr8247.6(spr6456 A_0)
at Spire.Pdf.PdfNewDocument.Save(Stream stream)
at Spire.Doc.Document.0(Stream A_0)
at Spire.Doc.Document.SaveToFile(Stream stream, FileFormat fileFormat)
at Spire.Doc.Document.SaveToStream(Stream stream, FileFormat fileFormat)
at Example.DocumentConverter.ConvertToPdf(Stream file) in
c:\Example\DocumentConverter.cs:line 20
at Example.FileConversionService.ConvertToPdf(FileUpload file) in
c:\Example\FileConversionService.cs:line 79
at Example.Functions.ProcessQueueMessage(FileConversionMessage message, TextWriter log) in
c:\Example\Functions.cs:line 64
at lambda_method(Closure , Functions , Object[] )
at Microsoft.Azure.WebJobs.Host.Executors.VoidMethodInvoker`1.InvokeAsync(TReflected instance, Object[] arguments)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.<InvokeAsync>d__0.MoveNext()