The tutorial is based on the BookWrapper demonstrations supplied with this COBOL development system. The BookWrapper is an intermediary program which maps .NET data types onto COBOL data types. It then calls the existing COBOL program, book.cbl, to perform the business logic.
In this tutorial you expose the functionality of the BookWrapper program as a WCF service and use a console application as a client to communicate with it.
A more sophisticated version of the demonstration shown here is supplied with the product samples. To check the code, start Visual COBOL Samples Browser, select Show managed only from the drop-down list at the top and open the WCF Book Service and Client demonstration in the WCG category.
As a prerequisite step for this tutorial, you need to build the BookWrapper and LegacyBook projects that are part of the WinBook sample. To do this:
The build creates two binaries, BookWrapper.dll and LegacyBook.dll, in the bin\Debug subfolder of the WinBook solution.
In order to create and configure a WCF service and then use a client to access it, you need to:
To start you need to create a solution to hold your WCF service and the client application:
You need to add the program that includes the functionality to expose as a service to your solution. To do this:
To enable your service to access the functionality in the BookWrapper and the LegacyBook projects, you must add them as project references to the WCFBook project:
interface-id WCFBook.IBookService attribute System.ServiceModel.ServiceContract(). method-id GetData public attribute System.ServiceModel.OperationContract(). procedure division using by value the-value as binary-long returning return-value as string. end method. method-id GetDataUsingDataContract public attribute System.ServiceModel.OperationContract(). procedure division using by value stockNumber as string returning return-value as type WCFBookWrapper.Book. end method. method-id Read public attribute System.ServiceModel.OperationContract(). procedure division using by value stockNumber as string returning theBook as type WCFBookWrapper.Book. end method. end interface. class-id WCFBookWrapper.Book attribute DataContract(). working-storage section. 01 BookTitle string property as "Title" attribute DataMember(). 01 BookType string property as "Type" attribute DataMember(). 01 BookAuthor string property as "Author" attribute DataMember(). 01 BookStockno string property as "StockNumber" attribute DataMember(). 01 BookRetail decimal property as "RetailPrice" attribute DataMember(). 01 BookOnhand binary-long property as "NumberOnHand" attribute DataMember(). 01 BookSold binary-long property as "NumberSold" attribute DataMember(). 01 StockValue float-short property as "StockValue" attribute DataMember(). method-id New. working-storage section. 01 newBook type WCFBookWrapper.Book. procedure division using by value stdbook as type BookWrapper.Book. set BookTitle to stdbook::Title set BookType to stdbook::Type set BookAuthor to stdbook::Author set BookStockno to stdbook::StockNumber set BookRetail to stdbook::RetailPrice set BookOnhand to stdbook::NumberOnHand set BookSold to stdbook::NumberSold set StockValue to stdbook::StockValue goback. end method. end class.
class-id WCFBook.BookService implements type WCFBook.IBookService. working-storage section. method-id GetData public. procedure division using by value the-value as binary-long returning return-value as string. set return-value to string::Format("You entered {0}" the-value) goback. end method. method-id GetDataUsingDataContract public. working-storage section. 01 book type BookWrapper.Book. 01 bookException type BookWrapper.BookException. procedure division using by value stockNumber as string returning theBook as type WCFBookWrapper.Book. try set book to type BookWrapper.Book::Read(stockNumber) set theBook to new WCFBookWrapper.Book(book) catch bookException raise new System.ServiceModel.FaultException(bookException::Message) end-try goback. end method. method-id Read public. working-storage section. 01 book type BookWrapper.Book. 01 bookException type BookWrapper.BookException. procedure division using by value stockNumber as string returning theBook as type WCFBookWrapper.Book. try set book to type BookWrapper.Book::Read(stockNumber) set theBook to new WCFBookWrapper.Book(book) catch bookException raise new System.ServiceModel.FaultException(bookException::Message) end-try goback. end method. end class.
Additionally, you need to configure your WCF service to call the BookWrapper code as follows:
<configSections> <!--The following code declares a section group for application configuration --> <sectionGroup name="MicroFocus.COBOL.Application"> <section name="Switches" type="System.Configuration.NameValueSectionHandler" /> <section name="Environment" type="System.Configuration.NameValueSectionHandler" /> </sectionGroup> <!--The following code declares a section group for run-time configuration --> <sectionGroup name="MicroFocus.COBOL.Runtime"> <section name="Tunables" type="System.Configuration.NameValueSectionHandler" /> <section name="Switches" type="System.Configuration.NameValueSectionHandler" /> </sectionGroup> </configSections> <MicroFocus.COBOL.Application> <Switches /> <Environment> <add key="dd_bookfile" value="%Public%\Documents\Micro Focus\Enterprise Developer\Samples\BookData\bookfile.dat" /> </Environment> </MicroFocus.COBOL.Application>
You need to configure the service endpoint details as follows:
This displays the general details of the endpoint - the address of the service, its binding and the service contract you want to expose - all known as the ABC of the endpoint. You need to configure the endpoint to use the IBookService contract you defined earlier.
This is the default endpoint that exposes metadata details about your service through the IMetaData Exchange contract.
Visual Studio provides a built-in WCF Service Host application and a WCF Test Client application to help you test your service without your own client application. This is enabled by default on the WCF Options tab in the project properties.
Visual Studio hosts your service in the WCF Service Host and loads it in the WCF Test Client application. The test client displays the endpoint for your service and the operations that are exposed.
This displays the operation details and enables you to test the operation with various parameters.
This displays the details about record 1111 in the BookWrapper application in the Response pane.
In the real world, you use various client applications to access services through their available endpoints. As part of this tutorial, you will create a simple managed console application that accesses the BookWrapper application through the WCF service contract and reads the records that it contains:
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.
You need to program your WCF client so that it uses the exposed operations of the WCF service reference. In this tutorial, the client uses the Read operation to access the information in the BookWrapper application and displays the record for a specified stock number.
program-id. Program1 as "WCFClient.Program1". data division. working-storage section. 01 stock-no string. 01 bookClient type WCFBookService.BookServiceClient. 01 myBook type WCFBook.Book. procedure division. invoke type System.Console::WriteLine("Enter a stock number and press Enter.") set stock-no to type System.Console::ReadLine() set bookClient to new WCFBookService.BookServiceClient set myBook to bookClient::Read(stock-no) invoke type System.Console::WriteLine(myBook::Title) invoke type System.Console::WriteLine(myBook::Type) invoke type System.Console::WriteLine(myBook::Author) invoke type System.Console::WriteLine(myBook::StockNumber) invoke type System.Console::WriteLine(myBook::RetailPrice) invoke type System.Console::WriteLine(myBook::NumberOnHand) invoke type System.Console::WriteLine(myBook::NumberSold) invoke type System.Console::WriteLine(myBook::StockValue) invoke type System.Console::WriteLine("Press Enter to exit.") invoke type System.Console::ReadLine() goback. end program Program1.
You need to configure the client so that it uses the WCF service endpoint and the service contract: