Creating Your First Web Service

Currency conversion is a common activity needed by Web applications. Because of the global nature of the Web, you can browse stores anywhere in the world. Normally, of course, those stores show prices in their local currency. You might need to convert these values into your local currency. In this section, you'll build the basis for a currency conversion Web service.

Open Visual Studio .NET. Create a new Visual Basic project of the type ASP.NET Web Service. Name the project CurrencyConverter and make sure that the server on which you create the project is a Web server running IIS 4.0 or higher and the .NET Framework.

After the project is loaded, you have a blank designer, much like you have seen before. If you look in the Solution Explorer window, you'll see that the current page is called Service1.asmx. .ASMX is the extension for a Web service. You'll also notice a file named Currency Converter.vsdisco. A .VSDISCO file is a discovery file, and the discovery process will be discussed later in this chapter.

Right-click on the Service1.asmx file in the Solution Explorer and choose Rename. Name the file CurrConvert.asmx. Notice that this just changes the filename; the service is still named Service1. Double-click on the designer of the CurrConvert.asmx file, and the code-behind file named CurrConvert.asmx.vb will open.

If you look at the code, you'll notice that a function is commented out. The function is a public function named HelloWorld(), but there is a strange element before the name of the function: <WebMethod()>. This marks the function as a method that is callable over the Web. Using the HelloWorld() example as a template, add the following code to the file:

<WebMethod()> Public Function ConvertCurrency(ByVal dAmount As Decimal, _
     ByVal sFrom As String, ByVal sTo As String) As Decimal
   Select Case sFrom
      Case "British Pounds"
         Return CDec(dAmount * 1.44333)
      Case "Japanese Yen"
         Return CDec(dAmount * 0.00859358)
   End Select
End Function

This code creates a method called ConvertCurrency. The method accepts three parameters: the amount of the currency to convert, the type of currency to convert from, and the type of currency to convert to. Inside the procedure is a simple Select Case statement that determines whether you are converting from British pounds or Japanese yen. Inside each case, you convert the amount into United States dollars.

In the real world, of course, the money could be converted to one of many countries. More importantly, you could query a database or some other resource to determine the current exchange rate, instead of hard-coding it as is done here. Again, the purpose of this exercise is merely to show you how Web services work.

Now, click on the Build menu and choose Build. This will build the Web service on the Web server. The service is now ready for testing.

Testing the Web Service

Visual Studio .NET provides a very simple way to test a Web service. Right-click on CurrConvert.asmx in the Solution Explorer window and choose View in Browser.

.NET creates a default page that allows you to work with methods in your service whose parameters can be input via HTTP POST or GET. The only option on the page is the one method you created: ConvertCurrency. Click on the ConvertCurrency link, and a new page appears. The new page allows you to enter values for any parameters in the method you created, and also contains a button to submit those values. Figure 9.1 shows what this form looks like.

Figure 9.1. Visual Studio .NET automatically generates a page that allows you to test your Web services.


Note

Although the page that is generated for you does have the ConvertCurrency method listed, there is also a discussion on the fact that your Web service is using http://tempuri.org as the default namespace. You will want to change this before distributing it. This will be discussed in more detail in Chapter 13, “Configuration and Deployment.”


You can see three text boxes in the middle of the page that allow you to enter values and then test them by using the Invoke button. Enter 100 in the dAmount box, British Pounds in the sFrom box, and US Dollars in the sTo box. Technically, in this example, it doesn't matter what you enter in the sTo box because your code never checks it. However, you can't leave the sTo box blank because the parameter is not optional. It helps to go ahead and put in an actual value for the “convert to” field.

When you click the Invoke button, the page passes the data to the Web service. The method runs and the data is returned in an XML stream that looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<decimal xmlns="http://tempuri.org/">144.333</decimal>

You can see that the return is a value, of the decimal data type, of 144.333. This means that 100 British pounds buys 144.333 U.S. dollars. If you run the form again but put Japanese Yen in the sFrom field, you will see that 100 yen buys approximately .86 U.S. dollars.

Immediately, you can see that the return from the Web service is an XML stream. What you can't see is that the call to the service is formatted as an XML stream as well. You'll learn more about this later.

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

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