What pollutes this switch statement?

I have a system that does the following:

  • Uploading documents to SharePoint
  • The event receiver will add the task to the database, create a folder for the task in the Document Conversion Catalog
  • Directory Watcher starts the Document Conversion Window Service
  • Windows service will receive a batch of 10 jobs from the DB (using the main thread)
  • When Windows service starts, creates X number of threads based on processor cores (using Parallel For)
  • Then creates a worker thread with timeouts for each db job (this differs from parallel for threads)
  • and he continues ...
  • In the process of converting ... to worker threads .. we call ActiveDirectory, register with DB (Read, Write) and upload the document back to SharePoint

I manage to break it ... if I upload a password protected document ... and shortly after uploading a PowerPoint document, the Powerpoint document throws an invalid password exception, etc.

but if the gap between both documents is even 60 seconds, everything works fine, which means the PowerPoint document is converted to PDF.

Below is the code, but I had to trim the unnecessary parts,

Here is the main class where it all starts

 Parallel.For(0, noOfThreadsToRunOnPDFServer, new ParallelOptions { MaxDegreeOfParallelism = noOfThreadsToRunOnPDFServer },
    i =>
    {
        this.docConvService.ProcessDocuments(i);
    });

      

Then the transformation happens here ...

using System;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Threading;

namespace PDFService
{
    public class AsposePDFConverter : IPDFConverter
    {
        private IDocConversionSettings settings;
        private ExceptionDispatchInfo conversionException = null;
        public enum SupportedExtensions
        {
            Doc,
            Docx,
            Xls,
            Xlsx,
            Pdf,
            Pps,
            Ppsx,
            Ppt,
            Pptx,
            Txt,
            Html,
            Mhtml,
            Xhtml,
            Msg,
            Eml,
            Emlx,
            One,
            Vsd,
            Vsdx,
            Vss,
            Vssx
        }

        public AsposePDFConverter(IDocConversionSettings settings)
        {
            this.settings = settings;
        }

        private void SyncThreadStartWithTimeout(ThreadStart threadStart, TimeSpan timeout)
        {
            Thread workerThread = new Thread(threadStart);
            workerThread.Start();

            bool finished = workerThread.Join(timeout);
            if (!finished)
            {
                workerThread.Abort();
                throw new ConversionTimeoutException("PDF Conversion exceeded timeout value");
            }
        }

        public MemoryStream ConvertToPDF(string documentName, Stream docContent, double timeoutMS)
        {
            this.conversionException = null;

            MemoryStream outStream = null;
            MemoryStream inStream = new MemoryStream();
            docContent.CopyTo(inStream);
            inStream.Seek(0, SeekOrigin.Begin);

            SupportedExtensions documentExtension;
            string szExtension = Path.GetExtension(documentName).TrimStart('.');
            if (Enum.TryParse(szExtension, true, out documentExtension))
            {
                switch (documentExtension)
                {
                    case SupportedExtensions.Doc:
                    case SupportedExtensions.Docx:
                    case SupportedExtensions.Txt:
                    case SupportedExtensions.Html:
                    case SupportedExtensions.Mhtml:
                    case SupportedExtensions.Xhtml:
                        SyncThreadStartWithTimeout(
                                () => { outStream = ConvertWordsToPDF(inStream); },
                                TimeSpan.FromMilliseconds(timeoutMS));
                        break;
                    case SupportedExtensions.Pps:
                    case SupportedExtensions.Ppsx:
                    case SupportedExtensions.Ppt:
                    case SupportedExtensions.Pptx:
                        SyncThreadStartWithTimeout(
                                () => { outStream = ConvertSlidesToPDF(inStream); },
                                TimeSpan.FromMilliseconds(timeoutMS));
                        break;
                }

                // Conversion happens on sub-threads so they can time out, if they throw an exception, throw it from this thread
                if (this.conversionException != null)
                    this.conversionException.Throw();

                return outStream;
            }
            else
            {
                throw new FormatNotSupportedException("Document type is not supported");
            }
        }

        private MemoryStream ConvertWordsToPDF(Stream docContent)
        {
            try
            {
                Aspose.Words.License lic = new Aspose.Words.License();
                lic.SetLicense(this.settings.AsposeLicensePath);
                Aspose.Words.Document doc = new Aspose.Words.Document(docContent);

                MemoryStream stream = new MemoryStream();
                doc.Save(stream, Aspose.Words.SaveFormat.Pdf);
                return stream;
            }
            catch (Exception ex)
            {
                this.conversionException = ExceptionDispatchInfo.Capture(ex);
                return null;
            }
        }

        private MemoryStream ConvertSlidesToPDF(Stream docContent)
        {
            try
            { 
                Aspose.Slides.License lic = new Aspose.Slides.License();
                lic.SetLicense(this.settings.AsposeLicensePath);
                using (Aspose.Slides.Presentation presentation = new Aspose.Slides.Presentation(docContent))
                {
                    MemoryStream stream = new MemoryStream();
                    presentation.Save(stream, Aspose.Slides.Export.SaveFormat.Pdf);
                    return stream;
                }
            }
            catch (Exception ex)
            {
                this.conversionException = ExceptionDispatchInfo.Capture(ex);
                return null;
            }
        }

    }
}

      

Mistake,

Error converting PDF document. Details: PDFConversionID: 6061, DocumentName: powerpoint.ppsx, WebURL: Disarmed, UploadedBy: REMOVED, ConversionDuration: 00: 01: 06.3072410

Aspose.Words.IncorrectPasswordException: Document password is incorrect. in Aspose.Words.Document. (Stream, LoadOptions)
in Aspose.Words.Document. (Stream, LoadOptions) to DocumentPDFConversionService.AsposePDFConverter.ConvertWordsToPDF (Stream docContent) to ...

As you can see, something very suspicious is going on

+3


source to share


1 answer


You are using the same instance of this.docConvService across multiple threads, so the conversionException property is probably written in a password protected document while your other document is processing. You have to create a new instance of your AsposePDFConverter or change how exceptions are returned, eg. in the result object returned by ConvertToPDF that contains the MemoryStream and your error.

A separate instance for each request:

Parallel.For(0, noOfThreadsToRunOnPDFServer, new ParallelOptions { MaxDegreeOfParallelism = noOfThreadsToRunOnPDFServer },
    i =>
    {
        new AsposePdfConverter(settings).ProcessDocuments(i);
    });

      



Returning a result-object:

 public ConversionResult ConvertToPDF(string documentName, Stream docContent, double timeoutMS)
{
    /** Your code **/
    return new ConversionResult() 
        {
        MemoryStream = memoryStream,
        ConversionException = conversionException
        };
}

class ConversionResult {
    MemoryStream MemoryStream {get;set;}
    ExceptionDispatchInfo ConversionException {get;set;}
}

      

+4


source







All Articles