RSCG–AMS – About My software –Multiple assemblies– part 3

The problem that I face now – and must be solved  – is what to do if I have multiple assemblies / dlls / asp.net core that wants to have the About My Software listed ? It will be a name conflict between the classes – or, if we put in different namespaces, will be difficult to find them to be listed .

For the second problem – it is relatively clear – we can have a Dictionary with the key AssemblyName and the value the instance of the AMS class for this assembly.

But how to initialize ?

First thing that I thought – static constructor . In the static constructor for AMS in the each assembly class – add to the above Dictionary the instance.

But , but … the static constructor is not called unless a class instance /static method  is called. So … ?

So ModuleInitializer to the rescue:

https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.moduleinitializerattribute?view=net-5.0

The code generated is now ( for an assembly with the name AMSConsole)

public class AboutMySoftware_AMSConsole : AboutMySoftware
{
    [System.Runtime.CompilerServices.ModuleInitializer]
    public static void Add_AboutMySoftware_AMSConsole()
    {
        AboutMySoftware.AllDefinitions.Add("AMSConsole", new AboutMySoftware_AMSConsole());
    }
    public AboutMySoftware_AMSConsole()
    {
        AssemblyName = "AMSConsole";
        DateGenerated = DateTime.ParseExact("20210624191615", "yyyyMMddHHmmss", null);
        CommitId = "not in a CI run";
        RepoUrl = "not in a CI run";
    }


}

The code to retrieve is modified like

Console.WriteLine("Show About My Software versions");
var amsAll = AboutMySoftware.AllDefinitions;
foreach (var amsKV in amsAll)
{
    var ams = amsKV.Value;

    Console.WriteLine($"{amsKV.Key}.{nameof(ams.AssemblyName)} : {ams.AssemblyName}");
    Console.WriteLine($"{amsKV.Key}.{nameof(ams.DateGenerated)} : {ams.DateGenerated}");
    Console.WriteLine($"{amsKV.Key}.{nameof(ams.CommitId)} : {ams.CommitId}");
    Console.WriteLine($"{amsKV.Key}.{nameof(ams.RepoUrl)} : {ams.RepoUrl}");
}

So far so good. Next implementation for WebAPI