|
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.
You can use the following solution and project name if you want.
|
Next, add a new ASP .NET web service application project that will host the web service for simple math operations.
You can use the following project name if you want for the web service application.
Next, add/edit the following Imports directive.
Imports System Imports System.Net |
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 |
Build the web service method individually.
Run the web service project individually in debug mode.
Select the second radio button to run the project without debugging.
The ASP .NET development server will be invoked and the browser will be launched. Click the Service1.asmx file.
The following screenshot shows the exposed methods from the web service. Let do some testing, click the Subtract method.
Key-in the values for a and b parameters and click the Invoke button.
The subtraction result will be displayed in the XML page. Make sure the result is correct.
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.
|
|
Click the Advanced button for the Add Service Reference page.
Click Add Web Service Reference button for the Service Reference Settings page.
Take note that the web service application was created in the same solution, so select the first link: Web services in this solution.
Then select the Service1 link, the only web service.
You can test the exposed methods here if you want. Leave the web reference name to 'localhost' and click the Add Reference button.
The added web service reference can be seen in the 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 |
Then, build the whole solution. Make sure there is no error for both project.
Run the solution without debugging.
Key-in two values and make sure the multiplication operation return a correct result.