VB.NET Uncovered: Services Rendered, Page 3
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.