Learning About Web Services

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.

Using a Web Service

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.

Figure 10.3. When you create a Web Service using .NET, the test page is created for you.


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.

Listing 10.6. XML Result from Calling the Add Function
<?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.

Figure 10.4. Use the Add Web Reference dialog box to add Web Services into your projects.


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.

Figure 10.5. The Web Service Description Language (WSDL) view of a Web Service is used by Visual Studio to create a local proxy of all of the Web Service's methods.


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.

  • XML— See the previous section. Extensible Markup Language is a means of encoding data, to include information about the data. It looks like, and is related to, HTML.

  • SOAP— A dialect of XML. That is, SOAP (not actually an acronym as it doesn't stand for anything anymore, but it used to mean Simple Object Access Protocol) is XML that complies with a specific Schema. SOAP is a means of providing a standard means of communicating between calls. You can think of it as a way of calling objects and their methods remotely, conceptually similar to DCOM.

  • WSDL— Another dialect of XML. WSDL (Web Services Description Language) is a means of describing the functionality provided by a Web Service. In one sense it is very similar to the idea of IDL (Interface Definition Language) used by COM in their type libraries. WSDL describes the available methods, and their parameters. As Visual Studio .NET 2003 generates WSDL documents based on a Web Service, you may never need to generate a WSDL document. However, you should have some knowledge of the information in a WSDL document.

  • UDDI— Universal Definition, Discovery, and Integration is (ready for this?) yet another XML dialect. In this case, UDDI is a query mechanism, and a directory of Web Services. A number of companies have together created the UDDI organization (http://www.uddi.org). Some of these companies offer directories of Web Services, some using XML Web Services, but also including other, non-SOAP-based services. Using UDDI, you can query these directories to find providers of Web Services.


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.

Listing 10.7. Once You've Added a Reference to a Web Service, Using it Isn't Any Different than Any Other .NET Class
Private Sub Button1_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Button1.Click

    Dim myMath As New _
        com.gotdotnet.samples.MathService
    MsgBox(myMath.Add( _
            CSng(TextBox1.Text), _
            CSng(TextBox2.Text)))
End Sub

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.

Creating a Web Service

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).

Listing 10.8. Creating a Web Service Is a Simple Task When You Are Using Visual Basic .NET
<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.

SHOP TALK: SHOULD I USE WEB SERVICES EVERYWHERE?

When developers hear the noise coming out of Redmond, and see just how easy it is to create and use Web Services, they want to use Web Services everywhere in their applications. In fact, I have seen some applications where all communication between the components is via Web Services. The argument these people use is that it gives the benefit of having a single method of creating, discovering, and calling the various parts of their application. Although this is undeniable, it is also undeniable that they are adding a huge amount of overhead to their application. Each time a Web Service is called:

  • The parameters must be converted to XML

  • The XML must be moved to the other component

  • The XML must be converted into .NET types

  • The function is called

  • The result must be converted to XML

  • The XML must be moved to the other component

  • The XML must be converted to .NET types

  • The original function now has its result

A lot happens when you call a Web Service!

So, when should you use a Web Service? I find it easiest if you think of Web Services as another user interface. Just as you might have a Web user interface that people can browse to, or a Windows interface people can use from their desktops, you can have a Web Service interface other applications can use. Use a Web Service when:

  • You will be communicating with another application

  • You need to communicate across the Internet

  • You need to communicate with code written on another platform (non-.NET)


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.134.130