< Adding the Web Service Reference (proxy class) | Main | ASP .NET, C# Web Service & C++ Program Example >

 


 

 

Chapter 11 Part 4:

XML Web Services and the Network

 

 

What do we have in this chapter 11 Part 4?

  1. Creating and Consuming the Web Service ASP .NET and VB .NET Program Example

  2. Creating the VB .NET Console Application

  3. Adding the ASP .NET Web Service Application

  4. Building and Running the Web Service Application

  5. Testing the Web Service Application

  6. Adding the Web Service Reference

  7. Build, Run and Test the Whole Project

 

Creating and Consuming the Web Service ASP .NET and VB .NET Program Example

 

In this exercise we will create two projects. The VB .NET console mode application will be used to send two numerical values to the web service application and display the received result while the ASP .NET web service application will receive the two numerical values, do the selected numerical operation and then return the result back to the console application. Take note that both projects will be created in the same solution.

 

Creating the VB .NET Console Application

 

Firstly, create a new console application project.

 

ASP .NET web service and VB .NET programming: new project creation

 

You can use the following solution and project name if you want.

 

ASP .NET web service and VB .NET programming: console application project

 

ASP .NET web service and VB .NET programming: VStudio project template

 

Adding the ASP .NET Web Service Application

 

Next, add a new ASP .NET web service application project that will host the web service for simple math operations.

 

ASP .NET web service and VB .NET programming: Adding new project to the existing project

 

You can use the following project name if you want for the web service application.

 

ASP .NET web service and VB .NET programming: adding the web service project

 

Next, add/edit the following Imports directive.

 

Imports System

Imports System.Net

 

ASP .NET web service and VB .NET programming: Adding the Imports directives

 

Add/edit the code given below. This web service application will expose four basic numerical operations through the WebMethod.

 

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports System.ComponentModel

Imports System

Imports System.Net

 

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

' <System.Web.Script.Services.ScriptService()> _

<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _

<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _

<ToolboxItem(False)> _

Public Class Service1

    Inherits System.Web.Services.WebService

 

    <WebMethod()> _

    Public Function Add(ByVal a As Double, ByVal b As Double) _

        As Double

        Return (a + b)

    End Function

 

    <WebMethod()> _

    Public Function Subtract(ByVal a As Double, ByVal b As Double) _

        As Double

        Return (a - b)

    End Function

 

    <WebMethod()> _

    Public Function Multiply(ByVal a As Double, ByVal b As Double) _

        As Double

        Return (a * b)

    End Function

 

    <WebMethod()> _

    Public Function Divide(ByVal a As Double, ByVal b As Double) _

        As Double

        If b = 0 Then

            Return -1

        Else

            Return (a / b)

        End If

    End Function

End Class

 

ASP .NET web service and VB .NET programming: adding the web service code in VB .NET

 

Building and Running the Web Service Application

 

Build the web service method individually.

 

ASP .NET web service and VB .NET programming: building the project

 

Run the web service project individually in debug mode.

 

ASP .NET web service and VB .NET programming: running the project in debug mode

 

Select the second radio button to run the project without debugging.

 

ASP .NET web service and VB .NET programming: web.config debug setting

 

The ASP .NET development server will be invoked and the browser will be launched. Click the Service1.asmx file.

 

ASP .NET web service and VB .NET programming: web service in action

 

Testing the Web Service Application

 

The following screenshot shows the exposed methods from the web service. Let do some testing, click the Subtract method.

 

ASP .NET web service and VB .NET programming: the exposed web service methods

 

Key-in the values for a and b parameters and click the Invoke button.

 

ASP .NET web service and VB .NET programming: testing the web service methods

 

The subtraction result will be displayed in the XML page. Make sure the result is correct.

 

ASP .NET web service and VB .NET programming: The result shown in XML page

 

Adding the Web Service Reference

 

Next, let complete the main console mode project. This project supposes to send two values to the web service, do the selected numerical operation and then return the result. Before that we need to add a reference to the web service previously created. Select the project folder > Right-click mouse > Select Add Service Reference menu. It is similar to adding the Imports directive for the .NET namespaces.

 

ASP .NET web service and VB .NET programming: adding the web service reference to the console application

 

Click the Advanced button for the Add Service Reference page.

 

ASP .NET web service and VB .NET programming: the Add Service Reference page

 

Click Add Web Service Reference button for the Service Reference Settings page.

 

ASP .NET web service and VB .NET programming: Service Reference Settings

 

Take note that the web service application was created in the same solution, so select the first link: Web services in this solution.

 

ASP .NET web service and VB .NET programming: selecting the desired web service

 

Then select the Service1 link, the only web service.

 

ASP .NET web service and VB .NET programming: seleting and adding the web service reference to the project

 

You can test the exposed methods here if you want. Leave the web reference name to 'localhost' and click the Add Reference button.

 

ASP .NET web service and VB .NET programming: Adding the selected web service to the existing project

 

The added web service reference can be seen in the Solution Explorer.

 

ASP .NET web service and VB .NET programming: The web service reference seen in the Visual Studio Solution Explorer

 

Next, open Module1.vb file for the console mode application. Add/edit the code as given below. In this example we just test the multiplication operation. You can add other three operations if you want.

 

Imports System

Imports System.Net

'Sample class that demonstrates calling a Web service

'with a custom HTTP header.

Module Module1

 

    Sub Main()

        'Get x,y from the console

        Console.WriteLine("Please input two values to be multiplied")

        Console.Write("First number:")

        Dim x As Integer = Integer.Parse(Console.ReadLine())

        Console.Write("Second number:")

        Dim y As Integer = Integer.Parse(Console.ReadLine())

        'Instantiate the derived math service

        Dim mathService As MyMathService = New MyMathService

        'Call the add method and print the result

        Dim result = mathService.Multiply(x, y)

        Console.WriteLine(x & " * " & y & " = " & result)

    End Sub

End Module

 

'MyMathService gets a WebRequest and sets the default credentials

'and a custom header on the request.  If you need control over the

'HTTP semantics of a Web service request it is much better to override

'the auto generated proxy class so that you don't lose the changes when

'the proxy gets re-generated.

Class MyMathService

    'WebServiceClientCS.localhost.Service1

    Inherits WebServiceClientVB.localhost.Service1

 

    'GetWebRequest is called before the SOAP request is sent

    Protected Overrides Function GetWebRequest(ByVal url As Uri) As WebRequest

        'Create a request based on the supplied URI

        Dim request As WebRequest = WebRequest.Create(url)

        'Set default credentials

        request.Credentials = CredentialCache.DefaultCredentials

        'Set a custom header value on the request

        request.Headers("custom-header") = "custom-value"

        Return request

    End Function

End Class

 

ASP .NET web service and VB .NET programming: adding the VB .NET code for the webrequest to the web service

 

Build, Run and Test the Whole Project

 

Then, build the whole solution. Make sure there is no error for both project.

 

ASP .NET web service and VB .NET programming: Building the VB .NET console application which request the ASP .NET web service

 

Run the solution without debugging.

 

ASP .NET web service and VB .NET programming: Running the VB .NET application which request the ASP .NET web service

 

Key-in two values and make sure the multiplication operation return a correct result.

 

ASP .NET web service and VB .NET programming: A sample output for the multiplication operation done by the ASP .NET web service which return the result to the console application

 

 

 


< Adding the Web Service Reference (proxy class) | Main | ASP .NET, C# Web Service & C++ Program Example >