Article
· 2 hr ago 4m read

Scripting with .Net core 10 and Iris SDK

One of the newest features of .Net core 10 with C# 14 is the file-based apps. This feature allows you to execute C# code in a simple .cs file without the need to create a solution, a project, or any of the related structure.

For example you can create a script.cs file using the notepad with the content: 

Console.WriteLine(“This is a script in c#.”);

Then in the command line or the terminal you execute the command:

dotnet run script.cs

There is plenty of information about this new feature of .net 10. To work with IRIS we can make use of the option to add NuGet package references directly inside the file. For example, to add the nuget for InterSystems IRIS, you include the following lines at the top:

#:package InterSystems.Data.IRISClient@2.5.0 

using InterSystems.Data.IRISClient;
using InterSystems.Data.IRISClient.ADO;

This allows the file to include the IRIS NuGet package and use the IRIS SDK. For example, below is a .cs file with a script to check the status of an InterSystems Interoperability production: 

#:package InterSystems.Data.IRISClient@2.5.0

using InterSystems.Data.IRISClient;
using InterSystems.Data.IRISClient.ADO;

//This script expects the namespace to connect
string irisNamespace = string.Empty;
if (args.Length > 0)
{
    irisNamespace = args[0];
}

if (string.IsNullOrEmpty(irisNamespace))
{
    Console.WriteLine("Please indicate the namespace to connect");
    return;
}

//Open a connection to InterSystems IRIS
IRISConnection conn;
IRIS iris;

try
{
    conn = new IRISConnection();
    conn.ConnectionString = $"Server = 127.0.0.1;Port = 1972; Namespace = {irisNamespace.ToUpper()}; Password = SYS; User ID = _system;";
    conn.Open();
    iris = IRIS.CreateIRIS(conn);
}
catch (Exception ex)
{
    Console.WriteLine($"Cannot connect to the interoperability server. Error message: {ex.Message} ");
    return;
}

try
{

    bool? isInteroperabilityEnabledNamespace = iris.ClassMethodBool("%Library.EnsembleMgr", "IsEnsembleNamespace");
    if (isInteroperabilityEnabledNamespace ?? false)
    {
        //The valid values are specified in the documentation
        //https://docs.intersystems.com/irislatest/csp/documatic/%25CSP.Documatic.cls?LIBRARY=ENSLIB&CLASSNAME=Ens.Director#METHOD_GetProductionStatus
        //And the numeric values can be found in the include file EnsConstants.inc
        Func<decimal, string> GetStateDescription = state => state switch
        {
            0 => "Unknown",
            1 => "Running",
            2 => "Stopped",
            3 => "Suspended",
            4 => "Troubled",
            5 => "Network stopped",
            _ => "Unknown state"
        };
        string currentInteroperabilityProduction = string.Empty;
        decimal currentInteroperabilityProductionState = 0;

        IRISReference currentProductionName = new IRISReference("");
        IRISReference currentProductionState = new IRISReference("");

        var status = iris.ClassMethodObject("Ens.Director", "GetProductionStatus", currentProductionName, currentProductionState, 2, 1);

        if (status.ToString() == "1")
        {
            currentInteroperabilityProduction = currentProductionName.GetValue()?.ToString() ?? "";
            currentInteroperabilityProductionState = currentProductionState.GetDecimal() ?? 0;
        }
        if (string.IsNullOrEmpty(currentInteroperabilityProduction))
        {
            //In the case the production is stopped, the call to GetProductionStatus doesn't return the production name
            //in this case we try to get the active production name
            currentInteroperabilityProduction = iris.ClassMethodString("Ens.Director", "GetActiveProductionName");
        }

        Console.WriteLine($"Active production in this namespace: {currentInteroperabilityProduction}");

        Console.WriteLine($"Production State: {GetStateDescription(currentInteroperabilityProductionState)}");

    }
    else
    {
        Console.WriteLine("The namespace is not enabled for interoperability");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error checking the state of the production in the namespace {irisNamespace}:{ex.Message}");
}
finally
{
    iris.Dispose();
    conn.Close();
    conn.Dispose();
}

Running this file with the parameter indicating the namespace in which you want to check the status of the production will verify if there is any production on the namespace and its current status:

PS C:\IrisScripting> dotnet run ScriptTest.cs INTBUS
Active production in this namespace: Integrations BUS
Production State: Running
PS C:\IrisScripting>

This new feature opens a new and interesting way to run scripts or small programs to automate tasks from the command line.

Discussion (0)1
Log in or sign up to continue