Tutorial: Object-Oriented WCF COBOL Service using SQL

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

Prerequisite

To complete this tutorial, you must have the WCF Tools feature installed in Visual COBOL. If this feature is missing, see To install missing features from the IDE for installation instructions.

Demonstration Solution

The OOSqlBookWCF 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, sqlbook.cbl is written in OO COBOL. The program reads and writes to the SQL Server BookDemo database that contains the book records.
ISqlBook.cbl
Defines the WCF service and data contracts. Methods that define the operations in the OOSQLBookWrapper 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 returned to the OOSqlBookWrapper program.
SqlBookService.cbl
The WCF service.
app.config
Defines service endpoints that enable the WCF service to call SqlBookWrapper code.
BookDemo Base Application
Important: The object-oriented version of the SqlLegacyBook project used in this tutorial, OOSqlLegacyBook, does not include the book-rec.cpy copybook. Instead, it includes an alternative, .NET version of book-rec-dotnet.cpy to define book-detail host variables. While the filename of this copybook is the same as the filename of the book-rec-dotnet.cpy copybook included in the object-oriented version of the SqlBookWrapper project (OOSqlBookWrapper), they are different files containing different code and each resides in the folder for the project to which it applies. These copybook files are named the same solely to indicate that in the OO version of the BookDemo application, they are both coded in terms of a .NET project rather than a legacy project.

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\OOSqlLegacyBook 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 > COBOL.
  3. Click the Windows Desktop category.
  4. In the center pane, select WCF Service Library.
  5. Complete the remaining fields as follows:
    Name OOSqlBookWCF
    Location Full path to any local directory; for example, c:\VSTutorials
    Solution Name OOSqlBookWCF
    Framework Choose the appropriate .NET Framework version from the drop-down list.
    Note: Must be .NET Framework 4.5 or higher.
  6. If the location you specified doesn't exist, check Create directory for solution.
  7. Click OK.

Add Existing Projects

These projects contain your application source code, etc.

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

Add Project References

  1. In the Solution Explorer, right-click the References folder in your OOSqlBookWCF project; then select Add Reference
  2. In the left pane, expand Solution; then click Projects.
  3. In the right pane, check OOSqlBookWrapper and OOSqlLegacyBook; then click OK.

Add Components

  1. In the Solution Explorer, delete the app.config, IService1.cbl, and Service1.cbl files from the OOSqlBookWCF 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\OOSqlBookDemoWCF\OOSqlBookWCF directory.
  5. Select the following files:
    • app.config
    • ISqlBook.cbl
    • SqlBookService.cbl
  6. Click Add.

Build the OOSqlBookWCF Project

  • In the Solution Explorer, right-click the OOSqlBookWCF 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 OOSqlBookWCF project; then select Edit WCF Configuration from the context menu.
  2. In the left pane of the Microsoft Service Configuration Editor, expand OOSqlBookWCF.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 (Empty Name) instance listed first.
  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 OOSqlBookWCF.dll.
  8. Select the OOSqlBookWCF.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. OOSqlBookWCF 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 OOSqlBookWCF project; then 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 GetDataUsingDataContract() 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 Read() operation using 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 OOSqlBookWrapper 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 Windows Desktop.
  3. In the center pane, select Console Application.
  4. In the Name field, type OOSqlClientWCF; 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 OOSqlClientWCF 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 OOSqlBookWCF; 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 OOSqlClientWCF project.
  2. Right-click the OOSqlClientWCF 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\OOSqlBookDemoWCF\OOSqlClientWCF directory.
  5. Select Program1.cbl; then click Add.

Configure Client Endpoint

  1. In the Solution Explorer, right-click the app.config file in the OOSqlClientWCF 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 OOSqlBookWCF project.
  5. Double-click OOSqlBookWCF.dll.
  6. Select the OOSqlBookWCF.IsqlBook contract; then click Open.
  7. Click File > Save; then close the Editor.

Run the Client

  1. In the Solution Explorer, right-click the OOSqlClientWCF project; then and 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.