Free Information Technology Magazines and eBooks

Friday, May 08, 2009

VB.NET: How to create ASP.NET web service

We are currently building an Auto Gate Project that compose of two sub systems. The queuing system was develop under Java and MySQL platforms while the Control system uses .NET 2.0 as front-end. To integrate the two different platforms, we use web service technology as the protocol for exchanging data. On this article, I will show you a simple web service that I hope will be helpful to beginners learning web service in VB.NET

To create your first web service project, just follow these steps:
1. Open your Visual Studio .NET and click File > New Website. This will display the project templates. Choose ASP.NET Web Service from the installed templates as shown below.

New Web Service Project

2. Visual Studio .NET should generate your first Hello web service method. Lets tweak the code and add another web service method.


Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
Inherits System.Web.Services.WebService

'<WebMethod()> indicates that this is a web service method
<WebMethod()> _
Public Function HelloFryan() As String
Return "Hello Fryan!"
End Function

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



if you can notice the obvious difference of our web service method with an ordinary VB method is this additional attribute before the function name:

<WebMethod()> _


It tells the framework that the following method is a web service.

3. To test the web service project, Click the Start Debugging icon (play icon). Visual Studio will automatically open your default web browser and display all available web service method. (in this case we have two web methods)

Test Web Service Project

4. Now lets test one of the web service method. Click the Addthis method. You should be forwarded to the following page:

Test Web Service Method

AddThis requires two parameters. Try to put numbers on each textbox then click Invoke. The web service method will return an XML result like the one below:

Web service result


There you have it, your first web service project. You can download the full sourcecode used on this project at Mediafire



For more ASP.NET tutorials, subscribe now

1 comments:

Wycliff said...

Great Tutorials.

Thanks so so much, every thing flows so well.

BTW, do you have some more material on ASP.NET, VB.NET web Services. Some kind of download-able e-book one can Buy. I really love the web service concept, but don't have sufficient tutorials.
my email is mwycliffe at gmail.com