.NET Framework and Web Services - Part 2

Creating a WebService Using VisualStudio.NET

We read about .NET Framework and Web Services Basics. Here we are going to learn how to create a WebService using VS.NET (using VB.NET.) and Consume the Webservice from VB.NET Client. This Article is based on VS.NET RC1 Release.

Step 1

Launch VS.NET IDE and create a new project, New project Dialog Box will open and select Project Type as a Visual Basic projects and Templates as ASP.NET WebService as shown below.


Then click "OK" . This will create a virtual directory under Default web site (IIS).

Step 2

Select Service1.asmx and double click that will take you to the code window. As shown below.  WebService will have .asmx extension.


In this code window we can add a method (Webmethod) that exposes to call over the Internet this type of method called WebMethod.

The Attribute WebMethod is very important. This will allow method to invoke over the Web using SOAP.

First we need to specify a WebMethod Attribute and the Function or Sub name,
for example: 

<WebMethod()> Public Function Add(ByVal Num1 As Integer, ByVal Num2 As Integer) As Integer
Return Num1 + Num2
End Function



In the code window edit Add and Div Web methods as shown above, and then build (compile) the project. If compiling successfully, view Service1.asmx in Browser. This will shows Add and Div methods. (Using HTTP-GET). This is generated by VS.NET.


Enter the num1 and num2 values click Invoke, this will invoke Add method in Service1.asmx using HTTP-GET (this is different from calling a method through SOAP ).



Using HTTP-GET we tested the service1.asmx webservice. This default Interface, which provided by VS.NET. Next we will see how to consume this same service using SOAP.

The following CODE SNIPPET gives the information about SOAP Request and Response Messages.


Consuming the WebService from VB.NET Client  

  1. First create a new VB.NET Console Application project. 
  2. Construct a SOAP Request Message as Shown below.


Every SOAP message has Envelope, Header, and Body. The above SOAP Request Message posted to
http://localhost/Mathfunctions/Services1.asmx URL.  The corresponding SOAP Response message is show below with Result.

Using POST Method SOAP Request Message is posted to the Web Server.

SOAP Response Message as a result.



In this article we understand how to Create and test Web Service Using VisualStudio.NET.