Welcome to the fourth part of VB.NET Uncovered – Services Rendered.
If you haven’t yet caught the first three instalments, check them out before continuing – Getting Started, Big Changes and Working the Web.
Now, back in the days of Visual Basic 6, we had COM. And what a wonderful thing that was – you could run code inside a separate file (perhaps a DLL or EXE) by writing just a few lines of code.
Then we got a little more technical and had DCOM, which was described as ‘COM on a longer wire’. This allowed you to run code inside a separate file on another machine somewhere in just a few lines of code.
With VB.NET, we get Web Services, which is effectively DCOM on an even longer wire.
Web Services allow you to call a method or function on a machine anywhere – over the Web, on a local intranet, on the same machine – and return a result.
The good news is you don’t have that “tight-coupling” of application and DCOM component that you had with VB6. In VB.NET, you just “discover” what a Web Service can do – and then use it. No messing around with GUIDs or type libraries or registering or any of that rubbish.
And today, we’ll be building our own groovy Web Service – then knocking together an application to use it. Wahoo – onward we go!
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?
So, we’ve seen how to create a simple Web Service and access it with a Web interface. But that doesn’t help us much in real life, does it?
Well, let’s create something that does:
- Create a new Windows Application in VB.NET:
Top Tip: It’s not just Windows Applications that can use Web Services. You can use the technology with Web Forms, too. In fact, you could even use Web Services within Web Services! In fact, you could even create Web Services that use Web Services that use Web Services! Erm, but I won’t
- Add a Button to Form1
- Change its Text property to: Get that Groovy Value, Cowboy!
Now we need to add a ‘reference’ to our Web Service:
- Click Project, Add Web Reference
- Type your Web Service address in the ‘Address’ text box
For example, I typed in: http://abydos/WebService1/Service1.asmx
This displays the same page as we’ve seen previously, plus contains a separate panel allowing you to view the ‘contract’ and ‘documentation’. This is all part of something called the discovery phase.
- Click the Add Reference button
Now let’s add some code to use our newly added Web Service:
- Double-click your Button
- Add the following code behind its Click event:
Dim objTest As New abydos.Service1() MessageBox.Show(objTest.ReverseName("Karl Moore"))
You may need to change the machine name here, however the core code should remain the same. We’re creating a new instance of Service1 on the Abydos server. We’re then running the ReverseName function, passing it the string ‘Karl Moore’ (or whatever) – and displaying what it returns in a MessageBox.
Top Tip: You’ll notice the objTest also has a bunch of other features, not just our programmed ReverseName Web Method. These are automatically implemented – check out the help for more information.
- Hit F5 and test your application
Notice how it all just seems to work, transparently calling the Web Service and returning the value? It’s not difficult – and all of that XML stuff is sorted for you.
Now, in this instance we were working with a local Web Service. Yet you could’ve been returning virtually anything from anywhere – and that’s the power of Web Services.
But what about versioning? What about compatibility? These are all the problems we’d typically experience with VB6 COM and DCOM applications.
Well, in VB.NET, they’re non-existent! No matter whether you add new functions or new parameters to existing methods – it doesn’t affect our application one bit. Only if you change the name of a function used by your application will you receive an error.
Oh, and if you remove the Web Service altogether. Yeah, that’s a bit fatal too.
And so there we have Web Services!
Sure, maybe this was a simple exercise. But it’s proven the possibilities behind this great new VB.NET feature.
Next up, try experimenting further. Add references to external Web Services currently in operation – such as those currently found at http://uddi.microsoft.com/vs.asp – then build test applications around that base.
Plus, build more complicated services and expose them to other machines on your network. You’ll be surprised at just how simple it really is.
Phew! Today, we’ve taken a lightning tour of Web Services, the more-flexible version of DCOM found in VB.NET.
We’ve created our own simple Web Service, explored it via a browser interface, then discovered and used it within our application.
Next, it’s time for you to explore and start pushing this technology to its limits – start creating your own services, use different parameter data types, find out how the whole process works internally via the help file.
In the fifth instalment, we’ll be looking at data access in VB.NET. It’s called ADO.NET, but what do you get? Find out next time.
And so until then, this is Karl Moore signing off for tonight, wishing you all a very pleasant evening. Goodnight!