Command-Line Interface Applications

The simplest user interface is the command-line application. It uses the console for its input and output. While not visually appealing, it often provides a fast and simple way to accomplish tasks. For our CLI application, we will have it call the HelloWorld Web Method and print the results. As you will recall, HelloWorld was modified in Chapter 1 to read its value from the Web.config file associated with the Web Service.

The first things we need to do are open up Visual Studio and create a new project. In Visual Studio, you will see CLI applications called console applications. Perform the following steps to create the console application:

1.
Select File, Project, New Project.

2.
Select the following items in the New Project dialog:

a. Project Type: Visual Basic Projects

b. Templates: Console Application

c. Name: Chapter2CLI

Figure 2.5 shows what the dialog should look like.

Figure 2.5. Setting up the Chapter2CLI application.


At this point, you have a skeleton for a console application. The IDE should have an opened file called Module1.vb. Our next task is to hook in the Chapter1 Web Service. To do this, add a Web reference to http://localhost/Chapter1/Chapter1.vsdisco and rename the namespace from localhost to Chapter1.

Your next step is to edit the Main subroutine. It will call HelloWorld, print the result to the console, and then exit. The edited code looks as follows:

Sub Main()
    Dim svc As New Chapter1.FirstService()
    System.Console.WriteLine(svc.HelloWorld())
End Sub

It does not get any easier than this. Figure 2.6 shows the output of this application.

Figure 2.6. Running Chapter2CLI.


I also promised you that we would look at the SOAP message exchange that occurs under the covers. The following is the request and response that occurred for our CLI application:

Request:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <HelloWorld xmlns="http://tempuri.org/" />
  </soap:Body>
</soap:Envelope>

Response:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <HelloWorldResponse xmlns="http://tempuri.org/">
      <HelloWorldResult>I love config files!</HelloWorldResult>
    </HelloWorldResponse>
  </soap:Body>
</soap:Envelope>

Let's move on to the next application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.151.45