When applying the license file (Aspose.Total Small Site Business) in my C# application I am getting the error:
Object Reference not set to an instance of an object
I thought the the application might not able to find or read the license file so to check that I made a small change to license file and the error changed to Invalid license signature. Please make sure the license file was not modified.
So this would suggest my app is reading the file which is good however when I go back to the non-modified license file the object reference error re-appears.
I have tried both the the file and stream options when setting the license.
So I am at a bit of a dead end - any suggestions would be much appreciated.
It seems you are encountering an “Object Reference not set to an instance of an object” error when applying the license file in your C# application. This issue can arise from several factors, and I can provide some suggestions to help you troubleshoot it.
Ensure Correct License File Path: Make sure that the path to your license file is correct. If the file is not found, it can lead to null reference errors. You can place the license file in the same directory as your executable for easier access.
Check License File Integrity: Since you mentioned that modifying the license file resulted in an “Invalid license signature” error, it indicates that the application can read the file. Ensure that the original license file has not been inadvertently altered. Even a small change, such as an extra line break, can invalidate the file.
Using the License Class: When applying the license, ensure you are using the SetLicense method correctly. Here’s a basic example of how to apply the license from a file:
Try Using a Stream: If you continue to face issues, consider loading the license from a stream. This can be useful if the license file is embedded as a resource in your application:
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace.Aspose.Words.lic"))
{
License license = new License();
license.SetLicense(stream);
}
Verify License Application: After setting the license, you can check if it has been applied correctly by accessing the IsLicensed property of the License class.
If you have followed these steps and are still facing issues, please provide more details about your implementation, and I would be happy to assist further.
Thanks, as mentioned I have tried these approaches and I still get the Object Reference error.
Re point number 5 - verify license application. This sounds like a good check to do however there does not appear to be a IsLicensed property of the License class and the only method I can see is setLicense. Can you provide more information on this check
I think the exception (e.g. “Object reference not set to an instance of an object” etc.) might occur when multiple threads try to access the license file concurrently during the worst case scenario, etc.
Please make sure you set the license only once per application/project. Please do not use license file in multi-threading as it is not needed or recommended. Ideally when your application starts, your licensing code should be processed at first (before using any other Aspose API) and then don’t re-process or set license again in the whole application life cycle.
Let us know with details and samples if you still find the issue. Also, which Aspose (.NET) APIs you are using and you encounter this issue? We will look into it soon.
Thanks @amjad.sahi for the quick response. Ok the threading is something I will look into.
The Aspose API calls are being made from within a SharePoint Event Receiver for a SharePoint on prem solution so I am not sure what the equivalent of Application Start would be?
The logic currently is:
Document is added to SharePoint
Event Receiver is fired for the ItemAdded event
The C# receiver code fires, the code sets the licence then makes the required Aspose API call.
The Event Receiver code ends
Obviously there could be multiple event receivers firing at once (e.g if multiple users are uploading documents).
I am transferring your thread to the appropriate forum, where one of our Aspose.Words team members will assess your issue and provide assistance accordingly.
@dev0314License.IsLicenseSet property has been removed from Aspose.Words public API a while ago due to security reasons. Usually it is not required to check whether the license is set, since Aspose.Words throws an exception when you attempt to set an invalid license or Aspose.Words cannot find the license file.
If you need to check whether the license is set, you can use code like this:
private static bool IsLicensed()
{
const string testString = "test";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write(testString);
using (MemoryStream ms = new MemoryStream())
{
doc.Save(ms, SaveFormat.Docx);
ms.Position = 0;
doc = new Document(ms);
}
string test = doc.ToString(SaveFormat.Text).Trim();
return test == testString;
}
Have you tried setting license in a simple console application? Is the exception thrown in this case?
Hi @alexey.noskov
Thanks for the suggestion. I have tried a simple console application and the license works without error. So at this stage it all points towards the SharePoint event receiver - can you advise how I can licence the API when calling from a SharePoint event receiver?
Thanks
@dev0314 Thank you for additional information. It looks like the problem occurs because the license is applied concurrently in multi-thread environment. So the only way to avoid this, I can suggest, is using lock. For example you can try using singleton pattern.
Thanks @alexey.noskov
I tried this however I still get the object ref error. Would it be possible to provide some multi thread examples that include setting the license instead of just the generic pages that were linked before.
Thread[] ts = new Thread[10];
for (int i = 0; i < ts.Length; i++)
{
ts[i] = new Thread(() => TestMultithreading.GenerateDocument($@"C:\Temp\{Guid.NewGuid().ToString()}.docx"));
ts[i].Start();
}
for (int i = 0; i < ts.Length; i++)
ts[i].Join();
public class TestMultithreading
{
public static void GenerateDocument(string outputFileName)
{
// Generate some dummy document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Hello, World!!!");
doc.Save(outputFileName);
}
static TestMultithreading()
{
// Set the license in the class static constructor.
lock (gLocker)
{
License lic = new License();
lic.SetLicense(@"C:\Temp\Aspose.Total.NET.lic");
}
}
private static readonly object gLocker = new object();
}