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.
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:
- Open Visual Studio.Net and select Create New Website under VB.NET.
- Select Web Service from the options listed.
- 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.
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).
Click the Invoke button, and the result will look like 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:
- Putting Web services to work
- Use .NET to develop and implement a Web service solution from start to finish
- Exposing product information via Web services
- Testing a Web service with a proxy class
- Integrating Web services into other applications
- Implement the components of an SOA with Web services
- Building scalable Web services
- Consuming .NET Web services with COM and VB6
- Determine when to opt for remoting over Web services
- TechRepublic Pro: Developer’s Guide to Web Services — ISO
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!