This document explains how to call a .NET library from Genero, using Genero and Web services, and IIS and Visual Studio .NET without any strong linkage between Genero and .NET. 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.
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(); } }
Start Visual Studio, and create a new web project with the name BarCodeService, as shown in the following image:
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:
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.
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.
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.
Iin 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
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.
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.
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
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.