The license does not apply all the time

Hello, we have a license for version 16.4.0.0 of Aspose.Words for .NET and we are having issues loading it. Sometimes the phrase “Evaluation Only. Created with Aspose…” appears on the generated document.
Do you have any idea how to fix this bug?

Here is the code that loads our license:

if (license == null)
{
    license = new License();
    if (!license.IsLicensed)
    {
        license.SetLicense("Aspose.Words.lic");
    }
}

@dicsitinfotho

It seems that you are experiencing issues with the license for Aspose.Words for .NET, where the “Evaluation Only” message appears intermittently. This can happen for several reasons, and I can provide some guidance on how to troubleshoot and resolve this issue.

  1. Ensure License File is Accessible: Make sure that the license file (Aspose.Words.lic) is located in a directory that your application can access. If the file is not found, the library will revert to evaluation mode.

  2. Correctly Load the License: Your current implementation checks if the license is already loaded before attempting to set it. However, it might be beneficial to ensure that the license is set every time your application starts. Here’s a revised version of your code:

    License license = new License();
    try
    {
        license.SetLicense("Aspose.Words.lic");
        Console.WriteLine("License set successfully.");
    }
    catch (Exception e)
    {
        Console.WriteLine("There was an error setting the license: " + e.Message);
    }
    
  3. Check License Validity: Ensure that your license is valid and has not expired. If you have a temporary license, it may have a limited duration.

  4. Use Full Path: If the license file is not in the same directory as your executable, consider using the full path to the license file when setting it:

    string licensePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Aspose.Words.lic");
    license.SetLicense(licensePath);
    
  5. Debugging: Add logging to check if the license is being set successfully. If there are exceptions, log the messages to understand what might be going wrong.

  6. Testing with a Simple Example: You can also test with a simple console application that only sets the license and creates a document to see if the issue persists.

If you continue to face issues after trying these steps, consider checking the Aspose documentation or reaching out to their support for further assistance.

Sources:
[1]: Apply License.cs
[2]: ExLicense.cs

@dicsitinfotho Please check the following points:

  • Make sure your call to SetLicense gets executed. Step through in the debugger.
  • Make sure your code does not catch an exception thrown by Aspose.Words licensing code. For example, Aspose.Words will throw if it cannot find the license.
  • Make sure the input documents do not already have the evaluation message. Aspose.Words does not delete existing evaluation messages.
  • Make sure SetLicense is executed before you instantiate any Document object.

Thanks for your reply. SetLicense is called before the document is initialized and does not raise any exceptions. The document is created dynamically via Aspose.Word and does not already contain the rating message.

Here is an extract of my code calling the license method:

public class EditionAspose
{
    protected static License license { get; set; }

    public DocumentEdition document { get; set; }

    public PdfSaveOptions optsPDF { get; set; }

    public EditionAspose()
    {
        Licence();
        this.document = new DocumentEdition();
    }

    public EditionAspose(string fichier)
    {
        Licence();

        System.Reflection.Assembly _assembly = System.Reflection.Assembly.GetExecutingAssembly();

        System.IO.Stream stream = _assembly.GetManifestResourceStream(fichier);


        this.document = new DocumentEdition(stream);

        stream.Dispose();
    }

    public static void Licence()
    {
        if (license == null)
        {
            license = new License();
            if (!license.IsLicensed)
            {
                license.SetLicense("Aspose.Words.lic");
            }
        }
    }
}

Our EditionAspose class corresponds to a parent class that allows us to then build the different editions we need for our software.

DocumentEdition inherits from Document :

public class DocumentEdition : Document
{
    public DocumentEdition() : base() { }
    public DocumentEdition(Stream stream) : base(stream)
    {
    }
}

We do not always reproduce the case. Sometimes the generated document will not contain the evaluation message, and other times it will.

@dicsitinfotho It is recommended to set license once per application domain. So, please try moving setting license into the class static constructor, like this:

public class EditionAspose
{
    public DocumentEdition document { get; set; }

    public PdfSaveOptions optsPDF { get; set; }

    public EditionAspose()
    {
        this.document = new DocumentEdition();
    }

    public EditionAspose(string fichier)
    {
        System.Reflection.Assembly _assembly = System.Reflection.Assembly.GetExecutingAssembly();
        using (System.IO.Stream stream = _assembly.GetManifestResourceStream(fichier))
            this.document = new DocumentEdition(stream);
    }

    static EditionAspose()
    {
        Licence lic = new Licence();
        lic.SetLicense("Aspose.Words.lic");
    }
}

I don’t understand your suggestion, we already handle it in our static method License() which checks if the license is instantiated and active :

public static void Licence()
    {
        if (license == null)
        {
            license = new License();
            if (!license.IsLicensed)
            {
                license.SetLicense("Aspose.Words.lic");
            }
        }
    }

@dicsitinfotho Yes, but this method is called each time a new instance of the EditionAspose class is created. A static constructor, on the other hand, is called only once when the class is used for the first time. Alternatively, you can move the license-setting code to the application startup.