Null reference in Aspose.Pdf.License.SetLicense(Stream stream)

Hi,

I got exactly the same problem exposed in this thread:

My error message is not really helping.

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

at

<span style="font-size:11.0pt;mso-bidi-font-size:10.5pt;font-family:"Calibri",sans-serif;

mso-fareast-font-family:Calibri;mso-fareast-theme-font:minor-latin;mso-bidi-font-family:
Consolas;mso-ansi-language:FR-CA;mso-fareast-language:EN-US;mso-bidi-language:
AR-SA">[1].Š.€<span style=“font-size:11.0pt;mso-bidi-font-size:
10.5pt;font-family:“Calibri”,sans-serif;mso-fareast-font-family:Calibri;
mso-fareast-theme-font:minor-latin;mso-bidi-font-family:Consolas;mso-ansi-language:
FR-CA;mso-fareast-language:EN-US;mso-bidi-language:AR-SA”>[1](Stream
)
at Aspose.Pdf.License.SetLicense(Stream stream)

It used to be only when I was resetting IIS, and the solution was to reset IIS again.

So sometimes, I needed to reset IIS two times in row…

But now, the problem is happening randomly , one or two times a month.

I can reset IIS to fix it.

I cannot share a piece of code, the system is quite complex.

Except:

FileStream fsPdf = new FileStream(pathLicenseAsposePdf, FileMode.Open, FileAccess.Read, FileShare.Read);

Aspose.Pdf.License licPdf = new Aspose.Pdf.License();

licPdf.SetLicense(fsPdf);

This is a really really annoying … you should find a solution … really…

I found post on this dating from 2012…

You cannot let your license system bugs … own your library.

Thank you very much!

Hi,

Thank you for contacting support. Please note, you need to set license once per application or process for each Aspose library. When you have applied a license as per application or process level, then you do not need to apply license again till the lifespan of the application. Aspose License should be applied in some common area of the application. For example, in the web application, you can set license in the start event of Global.asa file.

However, it is also not harmful to apply license multiple times in the code. Did you check the length of the license stream before calling a SetLicense method of the License class? Kindly prepare a small project application which reproduces this error in your environment, and then share a Zip of this project with us. We will investigate and share our findings with you. Your response is awaited.

Hi Briere,


Thanks for contacting support.

Adding more to Imran’s comments, when initializing the license through Stream instance, ensure to close the stream object properly and if you are trying to instantiate license at multiple locations, the file may get locked by one stream instance and a problem may occur when other instance/object tries to read it.

Hi all,

I also got the same issue in this post.
I tried checking the document is licensed before calling set license function.
I disposed stream as your recommend.
I tried to do other ways as using embedded file option, using directed link file.
But set license function still throw null object exception.
Before few months it’s worked normally. But now sometime it throw null exception.

Here is my small code to check license:
public void Activated()
{
try
{
Logger.Debug(“Start set license for Aspose pdf conversion”);
if (!Document.IsLicensed)
{
var pdfLicense = new License();
using (var stream = GenerateStreamFromString(AposePdfLicense))
{
pdfLicense.SetLicense(stream);
}
}
Logger.Debug(“Set license for Aspose pdf conversion successfully”);
}
catch (Exception ex)
{
Logger.Error(“Set license for aspose pdf conversion error:” + ex.ToString());
}
}

PS: I use aspose pdf product family version 17.2.0

Hi Toan,


Thank you for posting the details. We need to investigate the flow of the project application, like how you are passing out a license string, and then converting it to stream object. What are the values of these objects, when this error occurs in your environment? Kindly create a small project application which reproduces this error in your environment, and then share a Zip of this project with us. We will investigate and share our findings with you. Your response is awaited.

You can share the confidential files (e.g. license file) in private via an email as narrated in this help topic: Send your Confidential File to an Aspose Staff Member.