We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

SetLicense sporadically fails with NullReferenceException

We are using Aspose.Pdf (Version=10.5.0.0) to Combine Multiple PDF’s into Single PDF and while Setting The License file, we are getting below error.

We tried setting up the License file to read from the Physical file as well as File Stream but we are get the same error. This is intermittent error and the occurrence is not assured and comes randomly in the server.

Usually the issue gets resolved by doing App Pool Recycle and IIS reset which is not an feasible option always on production servers.

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

This issue was identified in older version of Aspose.pdf so we went with the Upgrade version hoping that it was fixed in latest versions.

Can you take a look at this issue.

Error 1:

System.NullReferenceException: Object reference not set to an instance of an object. at ?



..?(Stream ) at Aspose.Pdf.License.SetLicense(Stream stream)

Set Licence Memory stream failed

System.IndexOutOfRangeException: At most 4 elements (for any collection) can be viewed in evaluation mode. at Aspose.Pdf.PageCollection.get(Int32 ) at Aspose.Pdf.PageCollection.Add(Page , ?


) at Aspose.Pdf.PageCollection.Add(ICollection , ?


)

There seem to be multiple posts on this for older versions. The closest seems to be:

https://forum.aspose.com/t/129298https://forum.aspose.com/t/129298

Please note that this is getting called against a static variable, so it is basically the same thing as executing in Global.ASAX.

Hi Brandon,


Thanks for contacting support.

It appears that related query is posted here and we have already replied it. Please try following the instructions shared in other thread and in case you still face any issue, please feel free to contact.

I cannot see the thread you posted:

<a href="https://forum.aspose.com/t/25110v</a></p><p> </p><p>It says permission denied, even though I'm logged into the forums.</p>

Hi Brandon,

Looks like the other forum thread is marked private through another username. However I am copying the post contents here.

Recently some customers also encountered license initialization issue where license was not being initialized and in order to resolve this problem, a hotfix of Aspose.Pdf for .NET 11.1.1 was released. Please try using the latest release and in case you still face any issue or you have any further query, please feel free to contact.

We are using Version 11.4.0.0 and are still facing the same problem with setting the license as described by Brandon.


The only way to bring our application back to life is recycling the application pool, which is not a suitable method in a productive environment!

That issue seems to be a known one since 2012, and still not solved?

Hope we can get a fix for this problem soon.

Thanks
Regards
Marc

Hi Marc,


Thanks for your inquiry. Please note above suggested fix addressed some specific scenario. Please share your license file via email as suggested here and license implementation code, so we will look into your issue and will guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

Hi Tilal


Any News about this issue? Sent the necessary data a week ago.

Thanks for your support and reply.
Regards
Marc

Hi Marc,


Thanks for sharing the license file. I have tested your license and its working fine at my end. It seems it is license implementation issue in your application.

It is suggested to set license in some Application Level event. When you will set the license in application level event/scope, it will remain valid till the lifespan of application. Please try to set license as per your application structure and share the results.

  • If you are developing an ASP.NET application, you can call License.SetLicense from the Global.asax.cs (Global.asax.vb) file, in the Application_Start protected method. It is called once when the application starts. Do not call License.SetLicense from within Page_Load methods since it means the license will be loaded every time a web page is loaded.
  • If you are developing a Windows Forms or console application, call License.SetLicense in your startup code, before using Aspose.Pdf for .NET classes.
  • If you are developing a class library, you can call License.SetLicense from a static constructor of your class that uses Aspose.Pdf for .NET. The static constructor will execute before an instance of your class is created making sure Aspose.PDF for .NET license is properly set.
  • in case of WCF service, depending upon its hosting:
- if it is custom ServiceHost - then you can set license in service host start event
- if it is IIS - then set license can be called in Global.asax


We are sorry for the inconvenience.

Best Regards,

I’m sorry, but this is not an application problem. We continue to face this on the recommended upgrade version. Please listen to the voice of your customers. The fact that you can load the license is simply due to the fact that you are not testing under a multi-threaded scenario with proper load.

For all others interested, this appears to be a race condition inside ASPOSE related to both reading and writing of the license. Since the license appears to be static, we have mitigated much of the issue by putting locking semantics around setting of the license, and only doing it during application startup. Note that this does not fix the problem, it only reduces the frequency.

Thanks Brandon for adding your comment.


It is exactly what I see with our implementations. The hints in the previous post just point in the direction where we are already.

It is clear how and when to call SetLicense. The problem is that we follow the guidelines posted, and the situation’s still the same.

As we are too using a multithreaded environment, we give the lock’s a chance to at least decrease the frequency of that problem.

We really need a solution for this, and not a workaround.

Hi Marc/Brandon,


We are sorry for the inconvenience. As stated above that it seems license implementation logic issue in your application, upgrading to latest version will not fix it automatically. We will appreciate it if you please share a simple application here to reproduce the issue it at our end. So we will guide you accordingly.

Furthermore, please note we have observed the same exception in multi-threaded environment. The issue arises when we try to use a document in more than one threads. Aspose APIs are multithread safe as long as only one thread works on a document at a time. Different threads can safely work on different documents at the same time. So it is recommended to set a license once per application or process

We are sorry for the inconvenience caused.

Best Regards,

We are running version 17.1 and still running into this issue. Up until this point it had only been a minor annoyance. Sometimes when deploying we would just have to restart the application. However, last night the application got restarted on its own and was completely down until we discovered the licensing error this morning.

We are calling SetLicense on application startup as suggested. This happens as our PDF generator class is instantiated and added to the IOC container. Within the ConfigureServices method of the Startup class.

Can you please share a code sample outlining a safe way to implement this in a ASP.NET core application?

@czielin,
Kindly create a small web application which reproduces this problem in your environment, and then send us its Zip file for the testing purposes. We will investigate and share our findings with you.

In the process of putting together a project to reproduce the issue I believe I was able to fix it. I haven’t tested it in production, but putting the license check in a static constructor seems to get rid of the issue when testing locally. I will follow up if the issue pops up again in production.

I figured I may as well still post my project to reproduce in case you’d like to make this a bit more user friendly in future releases, or for someone else who may run into this thread in the future.

https://drive.google.com/file/d/0B28NGt4nOgzYU19ZSnhCTXc0Z2c/view?usp=sharing

This is a basic web application that reproduces the issue when the index action of the application is executed multiple times simultaneously as the application is starting up. The solution, again, was to move the license check from the instance constructor to a static constructor.

@czielin,

It is nice to hear from you that the problem has been resolved locally. Please deploy the code change in the production environment and let us know if you come across any issue. Please note, you just need to set license once per application or process for each Aspose library. When you will apply license as per application or process level, then you do not need to apply license again till the lifespan of the application. We recommend our clients to apply license in some common area of the application like application start event. However, it is not harmful to apply license multiple times.

Thanks @imran.rafique.

To be clear, your advice to set the license once per application appears to be sound (at least as far as I’ve been able to reproduce with testing). However, applying the license multiple times IS indeed harmful IF it is done simultaneously on multiple threads. As the included project shows. It will result in a NullReferenceException as the original poster on this thread noted, and all subsequent calls to the Aspose component will fail until the application is restarted.

So perhaps it’s safest to not tell people it’s OK to apply it more than once. Further, it would be nice if the license call were either made truly thread safe, or if multiple calls to set the license is disallowed altogether. Perhaps throw an exception if the license is attempted to be set more than once so the issue can be easily caught before it becomes a difficult to track down race condition that crops up sporadically in production. Will likely save significant time for your customers who find themselves in the same boat as myself.

@czielin,

If you are facing the null pointer error, then kindly create a small project application which reproduces this error in your environment, and then share a Zip of this project. We will investigate and share our findings with you.

Aspose.Pdf API is multi-threaded safe as long as only one thread works on a document at a time.

I included a link in my previous post to a simple project that consistently reproduces the issue.

“as long as only one thread works … at a time” - isn’t this the exact opposite of being thread safe?

“Thread safe: Implementation is guaranteed to be free of race conditions when accessed by multiple threads simultaneously.”

@czielin,

We are facing Microsoft.DotNet.Props was not found error in our environment when we tried to open your project. Please let us know the environment details, including Visual Studio (name & version) and some handy information which could help us to fix it. Meanwhile, we are investigating this error and will let you know once we are able to run your project in our environment.

I’m not familiar with that error. I used Visual Studio 2015 to create the project. You will likely need to make sure you have the version of the SDK specified in global.json installed on you machine (or change the global.json to match the SDK version you do have installed).
You will also need to add an Aspose PDF license file (“Aspose.Pdf.lic”) back into web project. I didn’t want to publicly post our license file.