VB.NET Uncovered: Services Rendered
Imagine what you could do with a Web Service.
Perhaps you could add a Web Service to your site that retrieved product availability? That would allow programmers to simply called the Web Service function and use the returned result in its processing.
Or maybe you don't deal with products. Perhaps you could be supplying parcel tracking information. Or stock quotes. Or interest rates. Or weather reports. Or auction lots. Or "DataSets" of properties for sale. Or whatever.
You could even use 'em to place orders.
So, Web Services are incredibly useful things. And they're incredibly easy to knock up, too.
In fact, they're so easy - we're going to do it right away:
- Create a new 'Web Service' project in VB.NET:
After plenty of whizzing and whirring, you should be shown Service1.vb in 'Design' mode. Web Services don't have a visual side - however you can use certain controls with them, hence the 'Design' interface.
Let's go into code mode:
- Double-click somewhere on the Service1.vb [Design] window
You'll be taken to the core code window.
Top Tip: Notice that as you work with Service1.vb, the Service1.asmx file is selected in the Solution Explorer. This .ASMX file is the actual 'path' to your service - the .VB file is the code behind it.
Once again, you'll notice a bundle of pre-generated code on the screen - including a handy sample HelloWorld function. Instead of running that one, let's quickly knock together our own:
- Add the following code underneath the 'HelloWorld' sample:
ReverseName(ByVal Name As String) As String ReverseName = StrReverse(Name) End Function
Now, the only difference between this function and a regular 'un is the
Anyway, this function merely reverses the Name parameter. Oh, whoop-de-doo. Nothing too exciting then.
Top Tip: In addition to a Function with parameters, you could also create a Sub with parameters. Or a Function without parameters. Or a Sub without parameters. Oh, the list just goes on and on. Actually, it doesn't. Erm, that's it.
- Save your project
- Click Build, Build on the menu to compile your Web Service
- Right-click on your Service1.asmx file in the Solution Explorer and select 'View in Browser'
This saves us having to load up Internet Explorer and mess around with all that jazz.
Now this page is automatically generated and acts as a 'user interface' to your service. You wouldn't usually access data this way, however it serves as a neat way for programmers to verify what information is available.
- Read the information provided about your ReverseName function
- Provide a Name parameter and hit the Invoke button
On my machine, I get the following returned in a new window:
<?xml version="1.0" ?> <string xmlns="http://tempuri.org/">erooM lraK</string>
Yours may be slightly different depending on your machine setup. Oh, and your name.
Now, as a lot of you will be aware, what has been returned here is XML. Now, XML is a method of "storing relational data" and is similar in structure to the way HTML is written. You can find out more about XML in our technology overview here (link to XML feature).
For this exercise, you don't need to know what XML is. However you should be aware that it is the language in which Web Services talk. Most of the time however, treat it like a street fight - you just don't need to get involved.
Top Tip: Web Services work via SOAP. This stands for the Simple Object Access Protocol and is an XML-based standard for communication through the HTTP protocol (meaning you can access data such as this through firewalls, etc). For more information, head down to www.microsoft.com/soap/
Anyway, away from the technical details - you can see that the string returned inside the XML is erooM lraK - my name reversed! Yours will probably hold a different reversed string. Unless your name is Karl Moore. Which is unlikely. Though not improbable. Sorta. Anyway. Ahem. Oh boy.
So, the Web Service works. We've created a function and accessed it via a Web interface. We've also seen the XML results it passes back.
But how can we use this in that crazy lil' thing called real life?
Page 2 of 4