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
Create a Solution and Project
- In Visual Studio, click
.
- In the
Create a new project dialog box, select
COBOL from the
Language drop-down list.
- In the center pane, select
WCF Service Library.
- 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.
|
- If the location you specified doesn't exist, check
Place solution and project in the same directory.
- Click
Create.
Add Existing Projects
These projects contain your application source code, etc.
- In the Solution Explorer, right-click the
OOSqlBookWCF solution; then select
Add > Existing Project.
- Browse to the
%PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\OOSqlBookWrapper directory.
- Double-click the
OOSqlBookWrapper COBOL project.
- In the Solution Explorer, right-click the
OOSqlBookWCF solution; then select
Add > Existing Project.
- Browse to the
%PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\OOSqlLegacyBook directory.
- Double-click the
OOSqlLegacyBook COBOL project.
Add Project References
- In the Solution Explorer, right-click the
References folder in your
OOSqlBookWCF project; then select
Add Reference
- In the left pane, expand
Solution; then click
Projects.
- In the right pane, check
OOSqlBookWrapper and
OOSqlLegacyBook; then click
OK.
Add Components
- In the Solution Explorer, delete the
app.config,
IService1.cbl, and
Service1.cbl files from the
OOSqlBookWCF project.
- Right-click the project; then select
Add > Existing Item.
- On the Add Existing Item dialog box, select
All Files (*.*) from the
Filename filter drop-down list.
- Browse to the
%PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\OOSqlBookDemoWCF\OOSqlBookWCF directory.
- Select the following files:
- app.config
- ISqlBook.cbl
- SqlBookService.cbl
- 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.
- In the Solution Explorer, right-click the
app.config file in the
OOSqlBookWCF project; then select
Edit WCF Configuration from the context menu.
- In the left pane of the Microsoft Service Configuration Editor, expand
OOSqlBookWCF.Service1; then click
Host.
- In the right pane under
Base Addresses, observe that the base address is local, as specified by the
http://localhost portion of the address.
- In the left pane, expand
Endpoints; then click the
(Empty Name) instance listed first.
- In the right pane, click the
Contract field; then click its browse button.
- Browse to the
\bin\Debug project subdirectory.
- Double-click
OOSqlBookWCF.dll.
- Select the
OOSqlBookWCF.IsqlBook contract; then click
Open.
- 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.
- 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.
- 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.
- In the left pane, double-click the
GetDataUsingDataContract() operation.
This populates the right pane with operation details, and enables you to test the operation.
- 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.
- Test the
Read() operation using alternative stock numbers. Valid values are
1111,
2222, and
3333.
- 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.
- In the Solution Explorer, right-click the solution; then select
Add > New Project.
- In the left pane, expand
Installed > COBOL; then click
Windows Desktop.
- In the center pane, select
Console Application.
- 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.
- In the Solution Explorer, right-click the
OOSqlClientWCF project; then select
Add Service Reference.
- In the Add Service Reference window, click
Discover.
- 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.
- In the Solution Explorer, delete the
Program1.cbl template file in the
OOSqlClientWCF project.
- Right-click the
OOSqlClientWCF project; then select
Add > Existing Item.
- On the Add Existing Item dialog box, select
All Files (*.*) from the
Filename filter drop-down list.
- Browse to the
%PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\OOSqlBookDemoWCF\OOSqlClientWCF directory.
- Select
Program1.cbl; then click
Add.
Configure Client Endpoint
- In the Solution Explorer, right-click the
app.config file in the
OOSqlClientWCF project; then select
Edit WCF Configuration from the context menu.
- In the left pane of the Microsoft Service Configuration Editor, expand
Client > Endpoints; then click
WSHttpBinding_ISqlBook.
- In the right pane, click the
Contract field; then click its browse button.
- Browse to the
\bin\Debug subdirectory of the
OOSqlBookWCF project.
- Double-click
OOSqlBookWCF.dll.
- Select the
OOSqlBookWCF.IsqlBook contract; then click
Open.
- Click
File > Save; then close the Editor.
Run the Client
- In the Solution Explorer, right-click the
OOSqlClientWCF project; then and select
Set as StartUp Project from the context menu.
- From the main menu, click
DEBUG > Start Debugging.
- When prompted, enter
1111.
This returns the data associated with stock number 1111.
- Press
Enter to exit the console application.