Tutorial: WCF COBOL Service using SQL

In this tutorial you expose the functionality of the SqlBookWrapper program as a WCF service and use a console application as a client to communicate with it.

Demonstration Solution

The SqlBookWCF solution and project you create for this tutorial uses the following provided demonstration files in addition to the BookDemo base application:

sqlbook.cbl
In this tutorial, the sqlbook.cbl program is recompiled to managed code without any changes. Recompiling the program exposes it as a class and exposes its main entry point as a static method. Its linkage section defines data as standard COBOL types, such as PIC X. Non-COBOL client programs do not understand these standard COBOL types. Therefore, you must map the standard COBOL types to .NET compatible types before communicating with the client program. This mapping is done by the SqlBookWrapper.cbl program.
ISqlBook.cbl
Defines the WCF service and data contracts. Methods that define the operations in the SQLBookWrapper program are marked with the System.ServiceModel.OperationContract() attribute. These are exposed via service endpoints. The SqlBookWrapperWCF.SqlBook class marked with the DataContract attribute exposes the data from the SqlBookWrapper program.
SqlBookService.cbl
The WCF service.
app.config
Defines service endpoints that enable the WCF service to call SqlBookWrapper code.

Populate the BookDemo Database

  • If you have not already done so in another OpenESQL Managed tutorial, using SQL Server Management Studio or another preferred method, run the SetupSqlBooksSQLServer.sql script, located in %PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\SqlLegacyBook directory (default location), against the BookDemo database.

    This generates and populates the appropriate table.

Create a Solution and Project

  1. In Visual Studio, click File > New > Project.
  2. In the New Project dialog box, expand Installed > Templates > COBOL.
  3. Select Managed.
  4. In the center pane, select WCF Service Library.
  5. At the top of the center pane, ensure that .NET Framework 4.5 is selected.
  6. Complete the remaining fields as follows:
    Name SqlBookWCF
    Location Full path to any local directory; for example, c:\VSTutorials
    Solution Name SqlBookWCF
  7. If the location you specified doesn't exist, check Create directory for solution.
  8. Click OK.

Add Existing Projects

These projects contain your application source code, etc.

  1. In the Solution Explorer, right-click the SqlBookWCF solution; then select Add > Existing Project.
  2. Browse to the %PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\SqlBookWrapper directory.
  3. Double-click the SqlBookWrapper COBOL project.
  4. In the Solution Explorer, right-click the SqlBookWCF solution; then select Add > Existing Project.
  5. Browse to the %PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\SqlLegacyBook directory.
  6. Double-click the SqlLegacyBook COBOL project.

Add Project References

  1. In the Solution Explorer, right-click the SqlBookWCF project; then select Add > Reference.
  2. In the left pane, expand Solution; then click Project.
  3. In the right pane, check SqlBookWrapper and SqlLegacyBook; then click OK.

Add Components

  1. In the Solution Explorer, delete the app.config, IService1.cbl, and Service1.cbl files from the SqlBookWCF project.
  2. Right-click the project; then select Add > Existing Item.
  3. On the Add Existing Item dialog box, select All Files (*.*) from the Filename filter drop-down list.
  4. Browse to the %PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\SqlBookDemoWCF\SqlBookWCF directory.
  5. Select the following files:
    • app.config
    • ISqlBook.cbl
    • SqlBookService.cbl
  6. Click Add.

Build the SqlBookWCF Project

  • In the Solution Explorer, right-click the SqlBookWCF project; then select Build from the context menu.

Configure Service Endpoint

This is the default endpoint that exposes metadata details about your service through the IMetaData Exchange contract.

  1. In the Solution Explorer, right-click the app.config file in the SqlBookWCF project; then select Edit WCF Configuration from the context menu.
  2. In the left pane of the Microsoft Service Configuration Editor, expand SqlBookWCF.Service1; then click Host.
  3. In the right pane under Base Addresses, observe that the base address is local, as specified by the http://localhost portion of the address.
  4. In the left pane, expand Endpoints; then click the first (Empty Name) instance listed below.
  5. In the right pane, click the Contract field; then click its browse button.
  6. Browse to the \bin\Debug project subdirectory.
  7. Double-click SqlBookWCF.dll.
  8. Select the SqlBookWCF.IsqlBook contract; then click Open.
  9. Click File > Save; then close the Editor.

Test the Service

Visual Studio provides a built-in WCF Service Host application and a WCF Test Client application that enable you to test your service without having to create a client application.

  1. SqlBookWCF should already be set as your startup project as evidenced by its bold appearance in the Solution Explorer; however, if it does not appear in bold, right-click the SqlBookWCF project; then and select Set as StartUp Project from the context menu.
  2. From the main menu, click DEBUG > Start Without Debugging.

    This starts the service and invokes the WCF Test Client window. The left pane shows the endpoint for your service and the operations that are exposed.

  3. In the left pane, double-click the Read() operation.

    This populates the right pane with operation details, and enables you to test the operation.

  4. In the Request pane, change the value for stockNumber to 1111; then click Invoke.
    Note: If a Security Warning appears, check In the future, do not show this message.; then click OK.

    This populates the Response pane with the data associated with stock number 1111.

  5. Test the operation again, substituting alternative stock numbers. Valid values are 1111, 2222, and 3333.
  6. Close the WCF Test Client window.

Create a Client Project

In the real world, you use various client applications to access services via their endpoints. As part of this tutorial, you create a simple managed console client application to access the SqlBookWrapper program through the WCF service contract, and query the database.

  1. In the Solution Explorer, right-click the solution; then select Add > New Project.
  2. In the left pane, expand Installed > COBOL; then click Managed.
  3. In the center pane, select Console Application.
  4. In the Name field, type SqlClientWCF; then click OK.

Add a Service Reference

Visual Studio provides an easy way to use a WCF service in a client application. The Add Service Reference functionality adds the service to the client project and generates a proxy for the client to use.

  1. In the Solution Explorer, right-click the SqlClientWCF project; then select Add Service Reference.
  2. In the Add Service Reference window, click Discover.
  3. In the Namespace field, replace the default namespace with SqlBookWCF; then click OK.

Edit the Client Application

You need to program your WCF client so that it uses the exposed operations of your WCF service. In this tutorial, the client passes the stock number as it invokes the Read() operation, and then displays the returned data.

  1. In the Solution Explorer, delete the Program1.cbl template file in the SqlClientWCF project.
  2. Right-click the SqlClientWCF project; then select Add > Existing Item.
  3. On the Add Existing Item dialog box, select All Files (*.*) from the Filename filter drop-down list.
  4. Browse to the %PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\SqlBookDemoWCF\SqlClientWCF directory.
  5. Select Program1.cbl; then click Add.

Configure Client Endpoint

  1. In the Solution Explorer, right-click the app.config file in the SqlClientWCF project; then select Edit WCF Configuration from the context menu.
  2. In the left pane of the Microsoft Service Configuration Editor, expand Client > Endpoints; then click WSHttpBinding_ISqlBook.
  3. In the right pane, click the Contract field; then click its browse button.
  4. Browse to the \bin\Debug subdirectory of the SqlBookWCF project.
  5. Double-click SqlBookWCF.dll.
  6. Select the SqlBookWCF.IsqlBook contract; then click Open.
  7. Click File > Save; then close the Editor.

Run the Client

  1. In the Solution Explorer, right-click the SqlClientWCF project; then select Set as StartUp Project from the context menu.
  2. From the main menu, click DEBUG > Start Debugging.
  3. When prompted, enter 1111.

    This returns the data associated with stock number 1111.

  4. Press Enter to exit the console application.

This concludes the WCF COBOL Service using SQL tutorial.