Web Services are applications that exist on a network. They communicate using standard Internet communication protocols (HTTP) via XML (rather, one particular dialect of XML called SOAP). The main benefit of Web Services over other means of distributing applications is that Web Services are not exclusive to any one platform or toolset. Web Services enable communication between different platforms, operating systems and/or programming languages. They are a simple, but powerful concept that allows developers to interconnect systems, even systems written using different tools and technologies.
Before you create a Web Service, you will use one first, in this case one of the Web Services made available at http://www.gotdotnet.com. In this case, you will use simple Math Web Service at http://samples.gotdotnet.com/quickstart/aspplus/samples/services/MathService/VB/MathService.asmx.
Open the URL in a browser, and you will see the page shown in Figure 10.3. This is a page generated automatically by the Web Service; you will see later how little you will need to do to make it work.
Click one of the methods to view a Web Form allowing you to test the method. Again, this page is automatically generated by .NET, no effort on your part. Notice that in addition to the form, there is documentation illustrating how to program against this Web Service. This will enable people to call your service, showing them what a correct call and return look like. To try the service, enter some values for the selected method and click Invoke. You should end up with XML, similar to the contents of Listing 10.6, containing the result.
<?xml version="1.0" encoding="utf-8"?> <float xmlns="http://tempuri.org/">7</float> |
Although the page created for the ASMX file is useful for testing, your users will not be using it to access your service. Instead, you will create a client (either a Windows Form or Web Form) that will communicate with the Web Service. Communication between this client and the Web Service works through an intermediate class, also known as a proxy. The proxy class, which Visual Studio generates for you when you add a Web Reference to your project, enables your code to call a Web Service as if it were just a regular object.
To try a Web Service from your own code, follow these steps:
1. | Create a new Windows Application called MathClient. You will use this application to test the Web Service. |
2. | Add a Web reference by right-clicking the MathClient project and selecting Add Web Reference. You should see the dialog box in Figure 10.4. |
3. | Enter the URL for the Math Service into the Address field, and click the Goto button (the green arrow). On the left side of the dialog box, you should see the initial form you saw earlier, followed by an item in the right side, and the Add Reference button should become enabled. |
4. | Click the Add Reference button to add a reference to this service to your project. |
Although the Add Web Reference dialog box shows you nothing but pretty Web pages, it is really working with something a bit more complicated. To see the “real” information being used when you added this Web Service reference, enter the URL for the math service into a Web browser, but this time append ?wsdl to the end of it. The result (see Figure 10.5) will be the XML specification for the Web Service, which is not quite as pretty as the normal display but it is very informative.
This is the WSDL for the Math service (see the sidebar, “More Acronyms Than You Can Shake a Stick At”) and it defines the properties and methods available from a Web Service.
SHOP TALK: MORE ACRONYMS THAN YOU CAN SHAKE A STICK AT (MATYCSASA)One thing XML has plenty of is acronyms. Once you start reading anything on XML, you might begin to doubt that such things as vowels exist. XML, SOAP, WSDL, and UDDI are just a few of the letters you may become familiar with as you drill into Web Services. For most Visual Basic .NET developers, though, these acronyms are optional reading. Visual Studio .NET 2003 makes working with Web Services as easy as dealing with any other component, and the XML fades into the background as an implementation detail. If, however, you like to know what you are playing with, here is a quick primer on these acronyms, and a few non-acronyms you will be hearing in the near future.
|
Once you add the Web Reference to the Math Web Service, you can use it just like any other .NET class.
5. | On your Windows Form (Form1), add a pair of text boxes and a button. Using those controls, the newly added Web Service, and the code from Listing 10.7, you can add the contents of the two text boxes together and display the result in a message box. |
Note that the namespace for your newly added Web Service defaults to the reverse of its Internet domain, although you can specify a different name in the Add Web Reference dialog box. Reversing the Internet domain is a pretty good way to name your Web Services though, as namespaces are generally listed in order from most general to most specific, which is what you get when you reverse an Internet domain name as well.
If you want, you can write additional code to try out the other functions available through the Math Web Service, but the general concept is the same no matter which service or function you are using.
You create a Web Service by creating a Web Service project in Visual Basic .NET, which is really just a variation on a Web Application and requires the same things (creates a new VRoot on a Web server, and so on). When you create a new Web Service project, you will start out with a single .ASMX file, which represents your new service. Within that file you can add new methods just like a regular class, but with one exception; if you add a <WebMethod()> attribute to a method in this class, it will be exposed as a Web Service.
To try this out for yourself, create a new method that returns the current date and time and mark it with the WebMethod() attribute (as shown in Listing 10.8).
<WebMethod()> _ Public Function GetCurrentTime() As Date Return Date.Now End Function |
Once you have created that method, if you press F5 to run your code, you will see the test page of your new Web Service. As with the Math service from GotDotNet, even with only a few lines of code the underlying functionality in ASP.NET has provided you with a working “home page” for your Web Service and a test page for each method you have created.
52.14.134.130