I'm trying to use the PdfToImageConverter.GenerateImage method concurrently, in various threads:

new PdfToImageConverter
{
    ScaleTo = this.settings.ScaleTo
}
.GenerateImage(
    pdfContentStream,
    task.Page,
    ImageFormat.Png,
    outputContentStream);

Usually, the method works fine, but sometimes it throws an exception:

System.Exception: Invalid license key
at NReco.PdfRenderer.License.a.B()
at NReco.PdfRenderer.PdfToImageConverter.b(A )
at NReco.PdfRenderer.PdfToImageConverter.A(A )

I set the license info once at the start of our service:

NReco.PdfRenderer.License.SetLicenseKey(
    "PDF_Renderer_Bin_Pack_....",            
    "Pc...30=");

Is the PdfToImageConverter.GenerateImage thread-safe, and how can I use the method in a multithreaded environment?

Thanks!

2

There are 2 answers

0
Sergey Voronkov On BEST ANSWER

The problem has been resolved in the latest version. We used outdated product version.

2
Vitaliy Fedorchenko On

This may happen when your license key initialization code (SetLicenseKey method call) is executed multiple times from several threads (in parallel). PdfToImageConverter can be used in multithread environment (each thread should use its own instance of the class).

SetLicenseKey should be executed only once (on the application start, from the 'main' thread); if this is not possible for some reason -- say, your service is Azure Function or AWS Lambda, you may use the following code snippet to guarantee a thread safety:

static object syncObj = new object();  // your class field

// code that initializes the license key
if ( NReco.PdfRenderer.License.GetLicenseKey()==null)
  lock (syncObj) {
    NReco.PdfRenderer.License.SetLicenseKey("owner_id", "key_value");
  }