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

Hello,

I'm using Aspose.Pdf.dll 7.8.0.

This is my code:

Stream license = Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + "." + "Aspose.Pdf.lic");

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

lic.SetLicense(license);

lic.Embedded = true;

I sometimes get this error:

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

at [1].ž.[1](Stream )

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

I’ve noticed in the release notes for version 7.4.0 the following issue: PDFNEWNET-33459 - SetLicense - throws exception randomly

It seems that you are aware of this issue and you have attempted to fix it in the past. I am now trying to reproduce it so that I can then try and implement a workaround. The trouble is that it happens very infrequently. I was wondering if you could suggest a scenario where this issue can be reproduced reliably.

Thank you,

Gabriel


Hi Gabriel,


Thanks for contacting support.

Similar issues related to license initialization were reported in past but I am afraid its pretty difficult to figure out the exact scenario in which it occurs. You also have observed that the behavior is not consistent. However we would like to share some suggestions in order to minimize such issues.

Please note that Aspose.Pdf initializes the license when first instance/object of Aspose.Pdf for .NET is created and its recommended to place the license initialization code n some static method which is executed once at that start of application. There is no need to re-initialize the license in multi-threaded environment when a new thread is created. The license is valid until the main application thread is running.

May be you can place the license initialization code in Main(), Page_Load() or any method which is executed at the start of application. In case you still face the similar problem or you have any further query, please feel free to contact. We are sorry for this inconvenience.

Hello Mr Shahbaz,

Thank you for your reply.

As you advised, I have placed the initialization code in a static method. Regrettably, the error still occurs:

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

at __.†.__(Stream )

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

We get this error approximately 2-3 times a month .

Could you please let me know when Aspose can provide a fix for this?

Thank you,

Gabriel

Hi Gabriel,


Thanks for your feedback. You are getting this issue due to license initialization in your specific scenario. Its difficult to suggest you anything without replicating the issue. Can you please identify the case in which you get the error message? So we will investigate and suggest you anything.

Moreover as already suggested above, It is recommended to apply the license only once per application domain and in case of Web applications to set license on Application_Start event of application. Can you please double check these recommendations?

We are sorry for the inconvenience faced.

Best Regards,

Hello,

We has the exact same problem and I think the problem is related to multithread. Our license was activated only the first time a call to an ASPOSE function was made (some kind of lazy loading activation). When used in a single thread, we never had any issue with it, but when we started testing in a multithread environement where a second process was activating ASPOSE while the first process didn’t complete the activation, we started receiving the NullReferenceException execption.

We fixed this issue by adding a lock statement and never saw the error again.

Simon

@simonbesner,
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. 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. You can create a small application which reproduces this error in your environment, and then share a Zip of this application. We will investigate and share our findings with you.

Hi,

I have the same issue with multiple threads and setLicense. I’ve created a simple test harness with unit tests and a parallel for loop which exhibits the issue. I’d prefer not to have to rewrite my application so please let me know who I can send my application to.

Thanks

Dave

@daverobinsone31d0,

There is an upload button in the header of the post editor and after pressing this button, you just need to browse and upload the Zip of the project from your computer. If the size of Zip file is more than 10MB, then please upload this file to any free file sharer server (e.g. Google drive) and share its download URL in the post.

AsposeTester.zip (170.5 KB)

Hi, attached is a zip file of the Visual Studio project. There is a test method Generate1000Documents which exhibits the issue whereby calling setlicense with multiple threads causes the issue. I’m using version 17.9.0.0 of Aspose.pdf. I’ve not included my license file for obvious reasons so this will need to be replaced.

Thanks

Dave

@daverobinsone31d0,

We have tested Generate1000Documents and could generate 1000 documents in the output directory without an error. We added a console project and execute this method as follows:

[C#]

UnitTest1 obj = new UnitTest1();
obj.Generate1000Documents();

Kindly send us your license file through a private message. We will investigate and share our findings with you.

Hi

Did you not try and just run the unit test through visual studio? I’m not sure why you would need to create a console project? I’ll send the license shortly. Did you use the same version, version 17.9? My stack trace is below after running the unit test through visual studio.

Test Name: Generate1000Documents
Test FullName: AsposeTester.UnitTest1.Generate1000Documents
Test Source: c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs : line 73
Test Outcome: Failed
Test Duration: 0:00:00.2776534

Result Message:
Assert.Fail failed. Exception: System.AggregateException: One or more errors occurred. —> System.NullReferenceException: Object reference not set to an instance of an object.
at .(Stream )
at .(String , Assembly )
at Aspose.Pdf.License.SetLicense(String licenseName)
at AsposeTester.UnitTest1.PDFGenerator(XmlDocument xmlDoc) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 101
at AsposeTester.UnitTest1.<>c__DisplayClass6.b__4(Int32 i) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 85
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_01.<ForWorker>b__1() at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask) at System.Threading.Tasks.Task.<>c__DisplayClass176_0.<ExecuteSelfReplicating>b__0(Object ) --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) at System.Threading.Tasks.Task.Wait() at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action1 body, Action2 bodyWithState, Func4 bodyWithLocal, Func1 localInit, Action1 localFinally)
at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, Action1 body) at AsposeTester.UnitTest1.Generate1000Documents() in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 83 ---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object. at .(Stream ) at .(String , Assembly ) at Aspose.Pdf.License.SetLicense(String licenseName) at AsposeTester.UnitTest1.PDFGenerator(XmlDocument xmlDoc) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 101 at AsposeTester.UnitTest1.<>c__DisplayClass6.<Generate1000Documents>b__4(Int32 i) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 85 at System.Threading.Tasks.Parallel.<>c__DisplayClass17_01.b__1()
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass176_0.b__0(Object )<—

—> (Inner Exception #1) System.NullReferenceException: Object reference not set to an instance of an object.
at .(Stream )
at .(String , Assembly )
at Aspose.Pdf.License.SetLicense(String licenseName)
at AsposeTester.UnitTest1.PDFGenerator(XmlDocument xmlDoc) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 101
at AsposeTester.UnitTest1.<>c__DisplayClass6.b__4(Int32 i) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 85
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.b__1()
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass176_0.b__0(Object )<—

—> (Inner Exception #2) System.NullReferenceException: Object reference not set to an instance of an object.
at .(Stream )
at .(String , Assembly )
at Aspose.Pdf.License.SetLicense(String licenseName)
at AsposeTester.UnitTest1.PDFGenerator(XmlDocument xmlDoc) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 101
at AsposeTester.UnitTest1.<>c__DisplayClass6.b__4(Int32 i) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 85
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.b__1()
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass176_0.b__0(Object )<—

—> (Inner Exception #3) System.NullReferenceException: Object reference not set to an instance of an object.
at .(Stream )
at .(String , Assembly )
at Aspose.Pdf.License.SetLicense(String licenseName)
at AsposeTester.UnitTest1.PDFGenerator(XmlDocument xmlDoc) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 101
at AsposeTester.UnitTest1.<>c__DisplayClass6.b__4(Int32 i) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 85
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.b__1()
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass176_0.b__0(Object )<—

—> (Inner Exception #4) System.NullReferenceException: Object reference not set to an instance of an object.
at .(Stream )
at .(String , Assembly )
at Aspose.Pdf.License.SetLicense(String licenseName)
at AsposeTester.UnitTest1.PDFGenerator(XmlDocument xmlDoc) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 101
at AsposeTester.UnitTest1.<>c__DisplayClass6.b__4(Int32 i) in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 85
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.b__1()
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass176_0.b__0(Object )<—
Result StackTrace: at AsposeTester.UnitTest1.Generate1000Documents() in c:\Users\daverobinson\Documents\Visual Studio 2012\Projects\AsposeTester\AsposeTester\UnitTest1.cs:line 92

Thanks

Dave

@daverobinsone31d0,

The project application has the reference of version 17.7. However, we have added reference of 17.9.0 of Aspose.Pdf for .NET API and unable to replicate the said errors in our environment. This is the snapshot: https://i.imgur.com/TXKzCQ0.png

Interesting. What OS are you running on and what version of Visual Studio etc? We are using Windows Server 2008 R2 Standard with Visual Studio 2012. I also tried using a console application on another machine and have only reproduced once. There is clearly a problem somewhere with multiple threads. Is there anything you can suggest as this does cause us problems in production. The only option at the moment for us is to make our application single threaded which defeats the object a little bit.

Thanks

Dave

@daverobinsone31d0,

We have tested all three use cases in Windows 8.1 Pro with Microsoft Visual Studio Community 2015 and Windows Server 2012 Datacenter with Microsoft Visual Studio Ultimate 2012. We are unable to replicate the errors in both these environments. We recommend you please try the latest version 17.12 of Aspose.Pdf for .NET API with this test project in the production environment, and then let us know how that goes into your environment.

Hi,

I’ve tested the latest version 17.12 on a seperate Windows Server 2012 machine and get the same error which is the call to setLicense. This is using a console app which does not rely on Visual Studio test framework etc.

I’m not sure what to do now as we can’t use this component in a multi threaded way? We may need to consider other tools for producing PDF’s.

System.AggregateException: One or more errors occurred. —> System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Collections.Generic.List1.Add(T item) at .() at .(Stream ) at .(String , Assembly ) at Aspose.Pdf.License.SetLicense(String licenseName) at AsposeTester.UnitTest1.PDFGenerator(XmlDocument xmlDoc) at AsposeTester.UnitTest1.<>c__DisplayClass7.<Generate1000Documents>b__5(Int32 i) at System.Threading.Tasks.Parallel.<>c__DisplayClass17_01.b__1()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass176_0.b__0(Object )
— End of inner exception stack trace —
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action1 body, Action2 bodyWithState, Func4 bodyWithLocal, Func1 localInit, Action1 localFinally) at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action1 body)
at AsposeTester.UnitTest1.Generate1000Documents()
—> (Inner Exception #0) System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Collections.Generic.List1.Add(T item) at .() at .(Stream ) at .(String , Assembly ) at Aspose.Pdf.License.SetLicense(String licenseName) at AsposeTester.UnitTest1.PDFGenerator(XmlDocument xmlDoc) at AsposeTester.UnitTest1.<>c__DisplayClass7.<Generate1000Documents>b__5(Int32 i) at System.Threading.Tasks.Parallel.<>c__DisplayClass17_01.b__1()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass176_0.b__0(Object )<—

Please advise as there is clearly an issue?

I’ve also managed to reproduce using the same test harness (using the console app like you did) on Windows 10. If it works first time and you run it again multiple times you generally get the error quite easily. I feel there is definitely a thread safety issue.

@daverobinsone31d0,

We have executed all test cases multiple times in our environment, but could not replicate the said errors. After the execution of all three test cases, it generates 1011 output PDF documents. Furthermore, you do not need to run SetLicense method in the multi-threaded way. You only need to apply license once per application or process for each Aspose assembly. When you have applied 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, e.g. application start event.

Hi,

I’m really surprised that you can’t reproduce it as I’ve easily been able to do so on different environments. I’ve tried it on multiple versions of your software also. Perhaps there is something different about your environment or you might have some ideas as to why??

I totally understand what you are saying about how you should call it but this approach lends itself to a scenario whereby you may have an application or site where a user click initiates the generation of some documents. Here you can control the process etc.

However our scenario is different from an application or website. We use BizTalk Server 2013 as our integration platform and we receive multiple request messages from either customers or internal application integrations. At any one time there may be a small amount of request messages or a large amount. Because the message could flow through multiple processes due to the nature of using something like BizTalk as an integration engine then you can’t necessarily control the process in the same way so we have to set the license each time we generate a message as each message goes through a process where we don’t have an ability to have a global call to set license. You could have one process for receiving messages, one for doing some business process and one for sending them. If we get a bulk load of messages then that’s when we have the problem. It doesn’t happen all the time but it’s maybe once a week.

I know you are saying you don’t need to run it in this way but in our case we do. So could you confirm if the way you are suggesting is the only recommended approach? If that’s the way you have to use it and you are saying you don’t support the multi threaded approach we are doing then that’s what I want from you guys.

If this is the case we either have to look another approach which would mean rewriting part of the process or throttling the delivery of outbound messages so we only attempt to produce one document at a time which will have a performance overhead particularly if we have hundreds of messages queued up. Otherwise we understand the limitations of Aspose and look at another tool.

Thanks for all your assistance so far it’s much appreciated. As we have a valid license is there any way to receive a call which may help you understand our scenario better?

Dave

@daverobinsone31d0,

Well, the multi-threaded way is supported. However, with your application project, we are unable to replicate the said errors and need to be more specific with your scenario. We need to identify the problematic difference of the environments. Kindly share the complete details from one of your system environments, including operating system name, local language settings, and some other handy information which could help us to replicate the same errors in our environment.

Once the errors are replicated in our environment, then it will be possible for us to investigate and fix them. We will prepare a VM as per your provided environment details, and then let you know about our findings. We do not provide support through the voice calls (in a free support model).

Hi,

Windows Server 2008 R2 Standard - Version 6.1.7601 Service Pack 1 Build 7601
Virtual machine - VMWare
Processor - 2x Intel® Xeon® CPU E5-2698 v3 @ 2.30GHz, 2297 Mhz, 2 Core(s), 2 Logical Processor(s)
Locale - United Kingdom
Timezone - GMT
RAM - 16GB

Software
.Net Framework 4.7
SQL Server 2012 (not really relevant I guess)
Visual Studio 2012 Update 5 (but again using the console app, this isn’t necessarily needed - only if you want to run unit tests)

I hope this is enough information to help you, if you need anymore then please let me know.

Dave