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.
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 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
Create a Solution and Project
- In Visual Studio, click
.
- In the
New Project dialog box, expand
.
- Click the
Windows Desktop category.
- In the center pane, select
WCF Service Library.
- At the top of the center pane, ensure that
.NET Framework 4.5
is selected.
- Complete the remaining fields as follows:
Name
|
SqlBookWCF
|
Location
|
Full path to any local directory; for example,
c:\VSTutorials
|
Solution Name
|
SqlBookWCF
|
- If the location you specified doesn't exist, check
Create directory for solution.
- Click
OK.
Add Existing Projects
These projects contain your application source code, etc.
- In the Solution Explorer, right-click the
SqlBookWCF solution; then select
Add > Existing Project.
- Browse to the
%PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\SqlBookWrapper directory.
- Double-click the
SqlBookWrapper COBOL project.
- In the Solution Explorer, right-click the
SqlBookWCF solution; then select
Add > Existing Project.
- Browse to the
%PUBLIC%\Documents\Micro Focus\Visual COBOL\Samples\SQL\ado.net\SqlLegacyBook directory.
- Double-click the
SqlLegacyBook COBOL project.
Add Project References
- In the Solution Explorer, right-click the
SqlBookWCF project; then select
Add > Reference.
- In the left pane, expand
Solution; then click
Project.
- In the right pane, check
SqlBookWrapper and
SqlLegacyBook; then click
OK.
Add Components
- In the Solution Explorer, delete the
app.config,
IService1.cbl, and
Service1.cbl files from the
SqlBookWCF 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\SqlBookDemoWCF\SqlBookWCF directory.
- Select the following files:
- app.config
- ISqlBook.cbl
- SqlBookService.cbl
- 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.
- In the Solution Explorer, right-click the
app.config file in the
SqlBookWCF project; then select
Edit WCF Configuration from the context menu.
- In the left pane of the Microsoft Service Configuration Editor, expand
SqlBookWCF.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 first
(Empty Name) instance listed below.
- In the right pane, click the
Contract field; then click its browse button.
- Browse to the
\bin\Debug project subdirectory.
- Double-click
SqlBookWCF.dll.
- Select the
SqlBookWCF.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.
- 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.
- 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
Read() 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 operation again, substituting 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 SqlBookWrapper 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
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.
- In the Solution Explorer, right-click the
SqlClientWCF project; then select Add Service Reference.
- In the Add Service Reference window, click
Discover.
- 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.
- In the Solution Explorer, delete the
Program1.cbl template file in the
SqlClientWCF project.
- Right-click the
SqlClientWCF 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\SqlBookDemoWCF\SqlClientWCF directory.
- Select
Program1.cbl; then click
Add.
Configure Client Endpoint
- In the Solution Explorer, right-click the
app.config file in the
SqlClientWCF 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
SqlBookWCF project.
- Double-click
SqlBookWCF.dll.
- Select the
SqlBookWCF.IsqlBook contract; then click
Open.
- Click
File > Save; then close the Editor.
Run the Client
- In the Solution Explorer, right-click the
SqlClientWCF project; then 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.
This concludes the WCF COBOL Service using SQL tutorial.