QA InfoTech » Business » Creating a Web service with VB.NET

Creating a Web service with VB.NET

Web services technology is based on HTTP, Simple Object Access Protocol (SOAP), and XML. Since Web services use open standards, calling Web services is fairly simple.

VB.NET allows you to use Web services as if they were entirely local objects since most of the marshaling between the client and the server is taking place in the background. This tip shows you how to create a simple Web service.

Example

I will create a Web service and add a Web function that will return the current machine’s IP address. Here are the steps for creating a Web service:

  1. Open Visual Studio.Net and select Create New Website under VB.NET.
  2. Select Web Service from the options listed.
  3. Once you get the code window open, add the following code:
<WebMethod()> _

Public Function GetMachineIPAddress() As String

Dim strHostName As String = “”

Dim strIPAddress As String = “”

Dim host As System.Net.IPHostEntry

strHostName = System.Net.Dns.GetHostName()

strIPAddress = System.Net.Dns.GetHostEntry(strHostName).HostName.ToString()

host = System.Net.Dns.GetHostEntry(strHostName)

Dim ip As System.Net.IPAddress

For Each ip In host.AddressList

Return ip.ToString()

Next

Return “”

    End Function

Once you debug the example, you will see a screen that looks like Figure A. Figure A

Figure A

It will list two available Web methods: the one that was generated (HelloWorld) and the one I created (GetMachineIPAddress). Click the link for GetMachineIPAddress, and you will see a screen allowing you to get more information about this method and invoking it (Figure B). Figure B

Figure B

Click the Invoke button, and the result will look like Figure C. Figure C

Figure C

 

About the Author