Back to Contents


How to Call .NET APIs from Genero in a SOA environment


Overview

This document explains how to call a .NET library from Genero in a SOA environment, using Genero and Web services, and IIS and Visual Studio .NET.
Notice that there is no strong linkage between Genero and .NET, and you can even call a .NET library from a non-Windows Genero platform.

For this tutorial we will use a .NET barcode creation library to build a picture from a numeric code, and C# as the development language. This will also work with any other .NET language.

Note: Accessing a Java library could be done in the same manner.

Back to the top


Prerequisites

Back to the top


Using the barcode library

This section depends on the library you want to use in Genero.  In our tutorial, we create one function called buildImage. The C# implementation is below :

Linear barcode = new Linear();
barcode.Data = code;
barcode.Type = GetBarcodeBuilderType(type);
barcode.AddCheckSum = true;
// save barcode image into your system
barcode.ShowText = true;
byte[] ret = barcode.drawBarcodeAsBytes();
if (ret != null) return ret;
else return null;

You will also need to convert the type of a code bar to the right type as expected by the library. Therefore, you will need the following function:

private BarcodeType GetBarcodeBuilderType(String str)
{
  if (str.Equals("CODABAR")) {
    return BarcodeType.CODABAR;
  } else if (str.Equals("CODE11")) {
    return BarcodeType.CODE11;
  } else if (str.Equals("CODE128")) {
    return BarcodeType.CODE128;
  } else if (str.Equals("CODE128A")) {
    return BarcodeType.CODE128A;
  } else if (str.Equals("CODE128B")) {
    return BarcodeType.CODE128B;
  } else if (str.Equals("CODE128C")) {
    return BarcodeType.CODE128C;
  } else if (str.Equals("CODE2OF5")) {
    return BarcodeType.CODE2OF5;
  } else if (str.Equals("CODE39")) {
    return BarcodeType.CODE39;
  } else if (str.Equals("CODE39EX")) {
    return BarcodeType.CODE39EX;
  } else if (str.Equals("CODE93")) {
    return BarcodeType.CODE93;
  } else if (str.Equals("EAN13")) {
    return BarcodeType.EAN13;
  } else if (str.Equals("EAN13_2")) {
    return BarcodeType.EAN13_2;
  } else if (str.Equals("EAN13_5")) {
    return BarcodeType.EAN13_5;
  } else if (str.Equals("EAN8")) {
    return BarcodeType.EAN8;
  } else if (str.Equals("EAN8_2")) {
    return BarcodeType.EAN8_2;
  } else if (str.Equals("EAN8_5")) {
    return BarcodeType.EAN8_5;
  } else if (str.Equals("INTERLEAVED25")) {
    return BarcodeType.INTERLEAVED25;
  } else if (str.Equals("ITF14")) {
    return BarcodeType.ITF14;
  } else if (str.Equals("ONECODE")) {
    return BarcodeType.ONECODE;
  } else if (str.Equals("PLANET")) {
    return BarcodeType.PLANET;
  } else if (str.Equals("POSTNET")) {
    return BarcodeType.POSTNET;
  } else if (str.Equals("RM4SCC")) {
    return BarcodeType.RM4SCC;
  } else if (str.Equals("UPCA")) {
    return BarcodeType.UPCA;
  } else if (str.Equals("UPCE")) {
    return BarcodeType.UPCE;
  } else {
    throw new Exception();                
  }
}

Back to the top


Calling .NET from Genero

Step 1: Create an ASP.NET Web Service Application

Start Visual Studio, and create a new web project with the name BarCodeService, as shown in the following image:

Step 2: Rename the generated files

Rename the generated class called Service1 with an appropriate name such as BarCode, and the file Service1.asmx to BarCodeService.asmx, for instance. The .asmx file is the file that is accessible from the IIS web server once the application is deployed. The .asmx file also contains a reference to the default generated class, Service1, which must also be renamed to the new name (BarCode in our tutorial), in case Visual Studio didn't make the change automatically.

The class view after renaming the class:

  

The file view after renaming the asmx file:

  

Step 3 : Add the barcode library as a reference

Right-click on the solution explorer, select Add Reference and use the Browse panel to enter the location of the barcode library called BarcodeLib.Barcode.dll:

 

Note: By default, the barcode library will be copied to the right place when deploying on the IIS web server.

Step 4: Add the buildImage method

Remove the default generated HelloWorld method, and create the buildImage method, as shown below.

Add the three using instructions to import the barcode library, and to declare buildImage as a WebMethod. Use the GetBarcodeBuilderType() method to convert a string to a code as expected by the barcode library.


Step 5 : Publish the service

Build the entire application, right-click on the solution, and select the publish operation. This will copy all necessary files to your IIS web server and make your application available at an URL, depending on where you deploy it on your IIS web server.

In our tutorial, the service will be located at the root of the server. In other words, it will be available at http://localhost/BarCodeService.asmx and the WSDL at URL http://localhost/BarCodeService.asmx?WSDL

 

Step 6: Generate 4GL stub to access the .NET library

Use the fglwsdl tool to generate the client stub to access the BarcodeService, as follows:

$ fglwsdl http://localhost/BarCodeService.asmx?WSDL

This will create two 4GL files, which must be compiled and linked into your 4GL application in order to call the .NET barcode library functions. These files contain the 4GL interface to access the .NET library where you will find the function buildImage, defined in 4GL.

Step 7: Modify your 4GL application

Modify your existing application, where you want to use the .NET library, by calling the 4GL functions generated in the stub. Then compile your application and the previously generated stubs, and link everything together.

Your application is now ready to use the different features of your .NET library.

Back to the top


Example 4GL program

This program calls the buildImage function of the Barcode .NET library.

GLOBALS "BarCode_BarCodeSoap.inc"
 
MAIN
  DEFINE wsstatus INTEGER
 
  IF num_args() != 3 THEN
    CALL ExitHelp()
  END IF
 
  LET buildImage.type = arg_val(1)
  LET buildImage.code = arg_val(2)
  LOCATE buildImageResponse.buildImageResult IN MEMORY
 
  LET wsstatus = buildImage_g()
  IF wsstatus <> 0 THEN
    DISPLAY "Error ("||wsError.code||") : ",wsError.description
  ELSE
    IF buildImageResponse.buildImageResult IS NULL THEN
      DISPLAY "Encoding failed"
    ELSE
      CALL buildImageResponse.buildImageResult.writeFile(arg_val(3))
    END IF
  END IF
 
  FREE buildImageResponse.buildImageResult 
 
END MAIN
 
FUNCTION ExitHelp()
  DISPLAY arg_val(0)||" <type> <data> <filename>"
  DISPLAY "type : barcode type such as EAN8 or CODE128"
  DISPLAY "data : data to be encoded with a barcode [0-9A-D]"
  DISPLAY "filename : resulting image filename"
  DISPLAY "exemple : createImage EAN8 12358723A mybarcode.jpg"
  EXIT PROGRAM (-1)
END FUNCTION

Back to the top


Conclusion

It is quite easy to interact with a .NET library from Genero using .NET Visual Studio and the web services. Of course you also need an IIS web server installed on your Windows system. This means that you can, in the same 4GL application, interact with .NET and Java libraries without any strong linkage between Genero and the third party libraries you want to use. This meets the SOA principles that provide more flexibility to your entire 4GL application.

You can integrate any new library from another vendor, without the risk of conflicts between different libraries that could happen if you had to link everything together in C or Java.

You can upgrade a third party library without recompiling the 4GL application, which will still work.

You can use all these third party libraries in other 4GL or other applications.

Back to the top