Document Conversion Suggested Architecture

Hi,

We have some architecture in place which converts different types of office documents into PDF, we developed a windows service which is interacting with SharePoint, DB, Active directory to perform some additional business logic and logging in addition to document conversion.

This setup for last few years is becoming a pain, e.g. most recently if I upload a password protected document and then shortly after a powerpoint document - powerpoint document throws this error,

Aspose.Words.IncorrectPasswordException: The document password is incorrect.
at Aspose.Words.Document. (Stream , LoadOptions )
at Aspose.Words.Document. (Stream , LoadOptions )

I think it’s related to multithreading and something to do with our code base as if I run same tests using just aspose products it all works fine.

Real Question :slight_smile:

I would like to change architecture of our code the way it works, I want to create a windows service which will take a stream or document converts it using Aspose and returns stream back - ALL using watch folders like ActivePDF document conversion do .

Do you guys have any architecture in place which we can follow for to go ahead with this please ?

Many Thanks

@muhammad.raja

Thanks for contacting support and sorry for the delay in response.

It seems that the issue might be related with the implementation of the multi-threaded routine, as while execution of such routine, it is a bit difficult to determine the root cause of the error. There is quite a chance that upcoming thread accesses the existing object of the document in the memory with new password value, which is why the API throws such error.

Furthermore, you may try to instantiate a new instance of your class in each thread which will avoid cross-threaded situations.

Aspose.Words API does support multi-threading and the only thing, you need to take care of is, only one thread should access Document Object in one time. For example, you may write a method in which a new instance of the Document is initialized and output is being saved in a global List<T> object, which can be accessed after the end of execution, to get all output streams/files.

Please check following code snippet where I have implemented a very basic and simple implementation of single thread for the document conversion.

System.Threading.ManualResetEvent FirstTier = new System.Threading.ManualResetEvent(false);
var th = new System.Threading.Thread(() =>
{
 // Load the document from disk.
 Document doc = new Document(dataDir + "Template.doc");
 dataDir = dataDir + "Template_out.pdf";
 // Save the document in PDF format.
 doc.Save(dataDir);
 FirstTier.Set();
});
th.SetApartmentState(System.Threading.ApartmentState.STA);
th.Start();

You may implement threading scheme as per your requirements, by following shared guidelines, and in case if you experience any issue, please feel free to let us know.