Talking to an IBM i (AS/400) from .NET: data queues, COBOL and a live web page
If you have ever heard "AS/400" and thought "that thing from the 80s", this article is for you. IBM i (the modern name of the AS/400) is still running payroll, banking and logistics in thousands of companies. Sooner or later, a .NET developer is asked to talk to it . By the end of this article you will know how to: get a free IBM i account and look around it; create a data queue (the IBM i…
AS/400 and IBM i are modern names for the same server operating system that has been running critical systems in businesses for decades. One way to communicate with this system from a .NET application is by using data queues, COBOL programs, and a web page interface.
A data queue is a message queue built into the system where programs can write and read messages. To interact with an IBM i, a free public server called PUB400 can be accessed using a 5250 terminal emulator. Establishing a connection involves connecting to the server's IP address (pub400.com) on port 23 (or 992 with SSL) and signing in via a command line interface.
Once connected, you can list data queues within a library using the WRKOBJ command and create a new data queue using CRTDTAQ. The maximum message length is limited to 30 bytes, which should be taken into consideration when designing your messages.
Reading messages from a data queue can be accomplished using SQL. IBM i exposes data queues as SQL table functions, allowing for easy interaction with ADO.NET connections. To retrieve a message, use the RECEIVE_DATA_QUEUE function, specifying the data queue name, library, and whether to remove the message from the queue upon retrieval. To send a message, use the SEND_DATA_QUEUE_BINARY function.
For .NET developers, a small open-source library is provided to simplify communication with the IBM i. This library consists of IDataQueueClient, IMessageCodec, IMessageHandler, IDeadLetterSink, a pipeline for handling messages, and a hosted service. The IDataQueueClient handles reading messages from the queue and sending them to the pipeline for processing.
IMessageCodec is responsible for converting bytes into objects, while IMessageHandler contains the business logic for processing each message. IDeadLetterSink is used to store messages that cannot be processed.
A simple .NET library example demonstrates how to register a consumer in an ASP.NET Core application. This involves adding singletons for IDataQueueClient, IDeadLetterSink, and configuring the DataQueueConsumer with the required message layout and processing options.
Finally, to call a COBOL program from C#, an example of a "Hello World" program in ILE COBOL is provided. This program takes a name as input and returns a greeting along with a return code. The message format is fixed-width, with specific fields for the name and greeting. The corresponding C# code uses a FixedWidthLayout to decode the incoming message and process it. If a message does not conform to the expected format, it is placed in the dead-letter store to avoid data loss.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.