Set or Validate License of Aspose.Words for .NET API | C# License Verification Code | IoC with DI Multiple Calls to SetLicense Method

1. I know that calling SetLicense multiple times is not required, but is there any drawback to calling SetLicense more than once?

2. Is there a way to tell that SetLicense has already been called on a given License object (I couldn't find one - and this would really be optimal, it would allow me to bypass the SetLicense call, and the load of the license file, altogether when it is not needed)?

The reason I ask these questions is that I have created a factory method to instanciate objects when I need them (kind of a simple IoC with DI for certain objects). Inside the factory method, if it is detected that I'm requesting an instance of an Aspose object, SetLicense is called against the appropriate License object.

This seems to work well in my unit testing, but I'm wondering if this could become a problem under load because of the multiple calls to SetLicense.

Kevin

Hi Kevin,

Thanks for your inquiry. I will answer your questions from Aspose.Words perspective.

1 - Calling License.SetLicense multiple times is not harmful, but simply wastes processor time.

2 - I am afraid, the License class doesn’t expose any built-in methods to check/track the status whether a license is properly applied or not. However, if you want to be sure, you can try creating your own custom method to do this job. To do this you could create a blank document and export this to a stream. Then reload this and check the first paragraph in the body for the evaluation message.

///
/// Returns true if the Aspose.Words license is set.
///
private static bool IsLicenseSet()
{
    // We will insert this text at the beggining of the document.
    // If the license set this text will be in th efirt paragraph,
    // if not an evaluation watermark will be in the first paragraph.
    const string text = "This is text used to check if the license is set";

    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.Write(text);

    // Save and open the document. If Aspose.Words works in evaluation mode it will add a watermark.
    using (MemoryStream docStream = new MemoryStream())
    {
        doc.Save(docStream, SaveFormat.Doc);
        docStream.Position = 0;
        doc = new Document(docStream);
    }

    // Check text of the first paragraph.
    return (doc.FirstSection.Body.FirstParagraph.ToString(SaveFormat.Text).Trim() == text);
}

Thank you for the reply Awais.It seems to me that using your method to check the License object before each SetLicense call would be more processor intensive than just blindly calling SetLicense.Here is the code for my factory method so you can see what I'm doing...

		/// 
		/// Create object instance - Use this method when the object's constructor requires parameters.
		/// 
		/// <typeparam name="T">System.Type of object to return
		/// <param name="InitializationParameters">object[] containing creation parameter values
		/// Object instance
		public static T Create(object[] initializationParameters)
		{
			string[] typeName = typeof(T).ToString().Split(new char[] { '.' });
			if (typeName.GetUpperBound(0) >= 2)
			{
				// Are we creating an Aspose object
				if (typeName[0].ToUpper() == "ASPOSE")
				{
					// Get Aspose.Total License file content
					// Aspose.Total license file is compiled as a resource in the Assembly
					// _LicenseStream is static, only read the resource if it has not already been read
					if (_licenseStream == null || _licenseStream.Length == 0)
					{
						_licenseStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("FileFormatConversion.Aspose.Total.lic");
					}
					_licenseStream.Seek(0, SeekOrigin.Begin);
					// Set Aspose license for return type
					switch (typeName[1].ToUpper())
					{
						case "CELLS":
							Aspose.Cells.License cellsLicense = new Aspose.Cells.License();
							//cellsLicense.SetLicense(_licenseStream);
							break;
						case "IMAGING":
							Aspose.Imaging.License imagingLicense = new Aspose.Imaging.License();
							//imagingLicense.SetLicense(_licenseStream);
							break;
						case "PDF":
							Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
							//pdfLicense.SetLicense(_licenseStream);
							break;
						case "WORDS":
							Aspose.Words.License wordsLicense = new Aspose.Words.License();
							//wordsLicense.SetLicense(_licenseStream);
							break;
						default:
							break;
					}
				}
			}
		T instance = (T)<span style="color: rgb(43, 145, 175);">Activator</span>.CreateInstance(<span style="color: blue;">typeof</span>(T), initializationParameters);
		<span style="color: blue;">return</span> instance;
	}
}

As you can see, I’m simply calling SetLicense on the appropriate License object (depending on which Aspose object is being instanciated) before actually instanciating the Aspose object.
Kevin

In the above, I have the actual SetLicense calls commented out because I don't actually have the license file yet (we just signed the PO to purchase 2 days ago).

Kevin

Hi Kevin,

Thanks for the additional information and you’re right about the code. Its just one way to check if the Aspose.Words license is already set or not. Please let me know if I can be of any further assistance.

Best regards,

kevin.d.rucker:
1. I know that calling SetLicense multiple times is not required, but is there any drawback to calling SetLicense more than once?
Hi Kevin,

Thanks for contacting support.

I would like to answer from Aspose.Pdf for .NET perspective.

There is no harm in calling license initialization code when referencing the license using string object. However when using Stream object, the problems might occur because the license file will be locked by one instance and other instance of Stream will try to read/load its contents.

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.

kevin.d.rucker:
2. Is there a way to tell that SetLicense has already been called on a given License object (I couldn't find one - and this would really be optimal, it would allow me to bypass the SetLicense call, and the load of the license file, altogether when it is not needed)?

I am afraid we do not have any method/property which can be used to to determine if license has been instantiated or not. In case you still face the similar problem or you have any further query, please feel free to contact.