Exception at SetLicense method

Hi Alexander,

This is really odd. Just to confirm, you have told that license works fine with Aspose.Words 6.0.1 and 6.2.0 and does not work with 6.1.0. Right?
Best regards.

Right. License works fine with all tested versions (6.0.0.0, 6.0.1.0, 6.1.0.0, 6.2.0.0) in my environment and client’s environment. But from some time described exceptions occured. Client’s hardware and software configuration doesn’t changed before exception and doesn’t damaged (already tested). I have no idea for this issue.
I tested SetLicense method and I see that method throws XML exception if Stream is new and empty. How about throwing some like “InvalidLicenseFormat” exception if any of which described exception occurs. I shall be able to provide a more readable error messages by handling this exception type.

Hi

Thanks for your request. I logged this feature request in our defect database as issue #7742. I will notify you what it is added.
Best regards.

Thanks. I have one question about described exception in first post. May be you be able to provide me any version about it - which error in license format may result to exception in this call stack position:
at System.Collections.ArrayList.get_Item(Int32 index)
And I shall try to detect which invalid data was send to SendLicense method. I just want to analyze any cause.

Hi

Thanks for your request. Unfortunately, I have no idea why this exception can occur. I tried to reproduce the problem on my side using different approaches but cannot get this exception.
Best regards.

Hi.
This client has another exception:

Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
Data: System.Collections.ListDictionaryInternal
Stack trace:
at ?.?.?(Stream ?)
at Aspose.Words.License.SetLicense(Stream stream)

And I founded this topic looks like similar to me problem: https://forum.aspose.com/t/123711
What are you think about it?

I think the reason for the problem is that you call SetLicense on multiple threads. The first thread might be loading an internal resource into a static variable and I now see that this operation is not guarded for multithreading in our code.
SetLicense by design should be called only once. Calling it multiple times was supposed to be safe but we never expected it to be called from different threads.
So you can fix the problem if you make sure SetLicense is called only once.
In the meantime I logged an issue #7879 to make it safe for multithreading just so nobody has this problem ever again.

I also want to answer the earlier question about throwing a license error specific exception class. I want to say that we will not make a special license exception class due to security reasons.

I am also getting this error. I have a solution that contains 3 console applications and a web service. I also created a pdf util class within the solution that handles all the Aspose stuff. So i am setting the license within that class…I am also threading the console apps and get this error when multiple threads are setting the license around the same time. Any idea when the next update will include thread safe SetLicense method or can you provide an example of setting the license one time in the console app but the aspose libraries are being referenced in a different class within the solution?

You can move license activation code to static constructor as a workaround.

Hi

Thanks for your request. The issue # 7879 is not resolved yet. You will be notified as soon as the issue is resolved. For now, you can try do as Alexander suggested.
Best regards.

Any status on this issue #7879?
“You can move license activation code to static constructor as a workaround.” - can you provide an example?

Example: you using this code now

class Class1
{
    public void DoSomething()
    {
        new License().SetLicense(...);
        ... some logic
    }
}

and now you can use this in this case:

class Class1
{
    static Class1()
    {
        new License().SetLicense(...);
    }
    public void DoSomething()
    {
        ... some logic
    }
}

So by doing this when spawning multiple threads (cause each thread will instantiate Class1), some threads will be instantiating Class1 at the same time, I will no longer have the contention when setting the license???

As I say before and Alexey - Yes, this is a temporary solution for this issue in case with multiple threads.

Hi,

I’m getting an exception while setting the license for Aspose.Words and I believe it might be caused by multithreading issues.

We use Aspose.Words (as well as other Aspose DLLs) in ASP.NET applications. In our enviroment multiple web applications access our DLL which contains a static method for setting the Aspose licenses.

I’m using Aspose.Words 5.3.0 and I noticed the latest release is 7.0.0. Was there any significant improvements regarding setting licenses between these releases?

Could I have access to some debug version of the Aspose.Words DLL in order to get more detailed information on the exception I’m getting? The stack trace I’m getting doesn’t say much.

Hi

Thanks for your request. There is not debug versions of Aspose.Words. So please provide me your stack trace.
Also, maybe you should apply the license in your Web application on Application_Start event as described here:
https://docs.aspose.com/words/net/licensing/
Best regards.

Hi,

These are examples of exceptions we’re getting:

Object reference not set to an instance of an
object. Stack: at x22bcffa34b42a390.x220f433da4115056.x7d0214bf69711dd9(Stream
xcf18e5243f8d5fd3) at
x22bcffa34b42a390.x220f433da4115056.x7d0214bf69711dd9(String x1c1fc72fe1a3b4ea,
Assembly x5807f920b6fc67c4) at Aspose.Pdf.Kit.License.SetLicense(String
licenseName)
Object reference not set to an instance of an object.
Stack: at Aspose.Pdf.?.?(Stream ?) at Aspose.Pdf.?.?(String ?, Assembly ?) at
Aspose.Pdf.License.SetLicense(String licenseName) 

Hi

Thank you for additional information. Have you tried to set license on Application_Start event in your Web applications, as I suggested earlier? Does the same exception occur with Aspose.Words or only with Aspose.Pdf.Kit and Aspose.Pdf?
Best regards.

We had what appears to be a similar issue. It took us several hours to diagnose. For what its worth, we now we have this code which works well for us.

We call AWord.SetLic(); wherever we need to.

Regards,

Doug

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading;

namespace bob
{
    class AWord
    {
        private static bool licensed = false;
        protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static private ReaderWriterLock _liclock = new ReaderWriterLock();

        public static void SetLic()
        {
            if (!licensed)
                try
                {
                    _liclock.AcquireWriterLock(60000);

                    if (!licensed)
                    {
                        Assembly assembly = Assembly.GetExecutingAssembly();
                        string[] rns = assembly.GetManifestResourceNames();
                        Aspose.Words.License license = new Aspose.Words.License();
                        foreach (string s in rns)

                            if (s.Contains("Aspose.Words.lic"))
                            {
                                using (System.IO.Stream st = assembly.GetManifestResourceStream(s))
                                {

                                    if (st != null && log.IsDebugEnabled)
                                        log.Debug("Setting AW license");
                                    if (st == null && log.IsDebugEnabled)
                                        log.Debug("Setting AW license ITS NULL!!!!");
                                    license.SetLicense(st);
                                }
                                licensed = true;
                            }

                    }
                }
                finally
                {
                    if (_liclock.IsWriterLockHeld)
                        _liclock.ReleaseLock();
                }
        }
    }
}