Project Management

Creating a Web service with VB.NET

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.

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

TechRepublic resources about Web services

Web services creation and usage represents a step in the direction of open standards. The myriad details about Web services are beyond the scope of this article. For more about Web services, check out these TechRepublic resources:

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.

----------------------------------------------------------------------------------------

Get Visual Basic tips in your inbox

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!

6 comments
nimblebits
nimblebits

This is a good example. I am learning to use Web Services with VB, currently most examples are in C Sharp but I prefer VB.NET. Thank you.

aispg8
aispg8

I have created an asmx.vb file and I need to have it scheduled. What the best way to accomplish this?

dhilden
dhilden

Nice resource... haven't tried anything with vb.net because it's much easier with php, javascript. Here's what I'm talking about: www.newviewit.com/Website-Programming-Services.html

jharvey6
jharvey6

...Dns.GetHostName() seems to be obsolete if your are running .NET 2.0

Justin James
Justin James

The biggest drawback to how easy this is in .Net, is that it is easy to wrap code that was never designed for this in a Web service wrapper. Make sure to really evaluate your code for security and performance before turning it into a Web method! J.Ja

Tony Hopkinson
Tony Hopkinson

It's a webservice, it responds to requests.. Scheduled how, you only want it to listen between 9 and 10 on Tuesday mornings? Schedule a request to the web service, is just make one of it, same as a web page would...