Developing an application for multiple run units in .NET COBOL

Note: The following applies to .NET COBOL only.

You can use any existing COBOL program in a multiple user environment in .NET. Do this as follows:

  1. Create a COBOL class library project and add your procedural COBOL program to that project.
  2. Rebuild the project.

    The COBOL program is now represented as a class that can be instantiated. It is packaged as a library (.dll); it is not compiled as an executable that can be run directly.

Note: .NET COBOL code that uses EXEC SQL statements (including program, class, enum, and valuetype code) must exist in a managed run unit with the exception of .NET code executing under the control of the SQL Server as SQLCLR stored procedures or types. This is because this code is automatically executed in managed Run Units, so no change is required for these applications.

If the code requires a multi-user environment or a thread pool environment for proper execution, then the use of managed run units is also recommended, but is not required provided that the code is engineered for thread safety.

Whether the COBOL program to be called is procedural or native OO COBOL, you need a program to call it, and this program can be in any .NET language, such as C# or COBOL:

  1. Create a project for the calling program, which can be more or less any project type, and add a reference to the:
    • Micro Focus Runtime (Interop RuntimeServices) assembly - this adds the MicroFocus.COBOL.RuntimeServices reference to your project.
    • Micro Focus Runtime assembly - this adds the MicroFocus.COBOL.Runtime reference to your project. You need this only if the calling program is in a .NET language other than COBOL.
    • Project containing the COBOL class library that you want to call.
  2. In the calling program, create a run unit using the MicroFocus.COBOL.RuntimeServices.RunUnit constructor.
  3. Use the Try ... Finally syntax to ensure that the program destroys the run unit if the program fails.
  4. Either use the RunUnit.Call() method to call the COBOL class, or:
    • Make sure that any parameters use .NET standard data types, and make sure that the called COBOL can receive these .NET standard data types.
    • Create an instance of the procedural COBOL program to be called.
    • Add the program instance to the new run unit, using the RunUnit.Add() method.
    • Invoke the program instance within the new run unit.
  5. Use the StopRun() method to destroy the run unit when you have finished with it.
Notes:
  • If the run unit causes a run-time error, a System.ApplicationException is thrown.
  • In native OO COBOL programs in a mixed language environment, do not use static methods and data as they cause run-time errors.
  • Ensure that data access is synchronized, as each run unit executes in a different thread.

For details of the RunUnit class in the MicroFocus.COBOL.RuntimeServices assembly, see: RunUnit Class.