GIS Converter -Unit Tests Projects -Set Aspose License.

Dani_S 5,241 Reputation points
2025-12-17T08:25:47.6266667+00:00

Hi Michel,

Thank you very much for your help !!!

I implemented all your remarks from ticket:

https://learn.microsoft.com/en-us/answers/questions/5658212/gis-cli-commands

1,How do I set the Aspose license to all my unit tests in one place ?

2.This the code I used in GisConverter.ConsoleApp

// Apply license once at startup

if (!AsposeLicenseManager.ApplyLicense())

{

Console.Error.WriteLine("⚠️ Failed to apply Aspose license.  Conversions may fail or produce watermarked output.");

Environment.Exit((int)ExitCode.AppError);
}

6.This code I used for manual testings is it a good practice?

warnings.txt

Thanks in advance,

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Le (WICLOUD CORPORATION) 7,700 Reputation points Microsoft External Staff Moderator
    2025-12-19T02:14:44.4366667+00:00

    Hello @Dani_S ,

    The current approach is okay, but you could also do something like this:

    private static void Main(string[] args)
    {
        Console.OutputEncoding = System.Text.Encoding.UTF8;
        args = EnsureDebugArgs(args);
        Environment.Exit(Run(args));
    }
    
    public static int Run(string[] args)
    {
        // apply license FIRST before doing anything else
        if (!AsposeLicenseManager.ApplyLicense())
        {
            Console.Error.WriteLine("failed to apply aspose license.");
            Console.Error.WriteLine("the program cannot continue without a valid license.");
            return (int)ExitCode.AppError; // <-- RETURN the exit code, don't kill the process
        }
    
        // help flags
        if (args.Length == 0 || args[0] == "help" || args[0] == "--help" || args[0] == "-h")
        {
            ShowHelp();
            return (int)ExitCode.Success;
        }
        if (args[0] == "--help-formats")
        {
            ShowHelpFormats();
            return (int)ExitCode.Success;
        }
    
        // extract the command from the first argument
        string command = args[0].ToLowerInvariant();
    
        try
        {
            switch (command)
            {
                case "gis_convert":
                    return RunGisConverter(args);
                default:
                    Console.WriteLine($"unknown command:  {command}\n");
                    ShowHelp();
                    return (int)ExitCode.AppError;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("error:");
            Console.WriteLine(ex.Message);
            return (int)ExitCode.Unexpected;
        }
    }
    

    Happy holidays.

    1 person found this answer helpful.

  2. Q&A Assist
    2025-12-17T08:26:01.3+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To set the Aspose license for all your unit tests in one place, you can create a base class for your unit tests where you apply the license in the constructor. This way, every test that inherits from this base class will automatically apply the license when the test class is instantiated. Here’s an example:

    public class BaseTest
    {
        public BaseTest()
        {
            if (!AsposeLicenseManager.ApplyLicense())
            {
                Console.Error.WriteLine("⚠️ Failed to apply Aspose license. Conversions may fail or produce watermarked output.");
                Environment.Exit((int)ExitCode.AppError);
            }
        }
    }
    

    Then, in your unit test classes, inherit from BaseTest:

    public class MyUnitTest : BaseTest
    {
        [Fact]
        public void TestMethod()
        {
            // Your test code here
        }
    }
    

    Regarding the code you used for manual testing, it is generally a good practice to check if the license is applied successfully and handle the failure gracefully, as you have done. However, instead of exiting the application, consider throwing an exception or logging the error to allow the test framework to handle the failure appropriately without terminating the entire process.


    References:


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.