SetLicense randomly throws "Object reference not set to an instance of an object" in Azure webjob

Hello,

I have a webjob deployed in Azure in which we are using Aspose.PDF version to 17.10.0 to load and perform various logic on pdf document. The issue is that the SetLicense is constantly and randomly throwing this error:

Error - Object reference not set to an instance of an object.Stack: at ​ .(Stream )
at Aspose.Pdf.License.SetLicense(Stream stream)

I have read through all the related posts and have since changed my code to set the license using the a Stream, and I am performing this step immediately before using the Aspose.Pdf.Document object. This seemed to resolve most of the issues and the error frequency was reduced. Setting the license in the constructor of the class that i am using almost always guaranteed the error and that is why i moved it out.

var data = File.ReadAllBytes(_asposeLicPath);
var licenseStream = new MemoryStream(data, 0, data.Length);
licenseStream.Position = 0;
var lic2 = new License();
lic2.SetLicense(licenseStream);
licenseStream.Close();

But now the error is still happening consistently and failing many items in our queue. The webjob itself is running continuously and it will launch the default of 16 processes based on queue messages. It will also retry each item up to 5 times. I have seen items fail on this error 5x in a row, an then later i can process it without a problem. In the calling function i am instantiating a new instance of the class where we are using Aspose and setting the license, so it should be running on its own thread. This seems to be an ongoing issue with multi-threaded processes or concurrent/parallel processing. Can you offer any assistance or direction on this? Or if there is fix on the horizon?

Thanks

@mwillisad6a8

Thanks for contacting support.

The reason you are facing this issue is because you are instantiating license object in each thread which tries to access a physically located license file. Please do not set license in each call of the method, instead you can set it only once when application starts.

You can call License.SetLicense(...) in the Global.asax.cs file, in the Application_Start protected method. It is called once when the application starts. Also, please do not call License.SetLicense(...) in Page_Load methods, since it means the license will be loaded every time a web page is loaded.

In case you still any issue, please let us know.