Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Jul 30, 2015 12:59 am

I have created an Azure WebJob that converts Word documents to PDFs. The code that performs the conversion looks like this:

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()

bradleywebber
 
Posts: 1
Joined: Thu Jun 04, 2015 7:47 am

Thu Jul 30, 2015 8:55 am

Hello,

Thanks for your inquiry.
Sorry that it is not thread-safe when convert DOCX to PDF with multi-thread, currently there is only way to lock it, and we always optimize our product to be thread-safe.

Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Return to Spire.Doc