Consume an English to Pig Latin Web Service
By steve schwab
Published: 4/2/2002
Reader Level: Beginner
Rated: 4.00 by 1 member(s).
Tell a Friend
Rate this Article
Printable Version
Discuss in the Forums

Recently, I submitted a Web Service and thought a few of you would be interested in how I made it. I am aware of the high standards of the articles submitted to this site and of the productivity gains Web Services will bring to future Web Applications. My English to Pig Latin Service serves no purpose whatsoever other than to have fun and learn about Web Services.

Web Services are basically code modules or classes that can be accessed on a networked computer. Instead of having to make a class that performs some function - such as getting a news article or making a graph - I can find an exsisting class that already does this. This class does not have to be made by someone in my team, company or country. A company who has the expertise in a particular realm can expose this as a Web Service on the Internet, and our team can use it as a building block of our application.

Lets say you are making an application on Federal Tax Guidelines. It is decided, perhaps in desperation, that a section translating English to Pig Latin would be useful. Budgets being what they are, there is no time to build this functionality. In fact you receive an e-mail saying "Marketing needs an English to Pig Latin Translator this afternoon. I saw the English to Pig Latin Web Service on the DotNetJunkies.com site"

The great thing about Web Services is they can be made using many different operating systems or programming languages. I happened to choose ASP.NET to build a Web Application with VB.NET as the programming language and VisualStudio.NET as the development environment.

Building the component

  1. Create a new ASP.NET Web Application using VB.NET
  2. Select File > New Item > Web Service
  3. Name pigLatin.asmx and open up the code view page
  4. Here is where the code for your class goes. The public method that you wish for the World to call, starts with-
    <WebMethod()> Public Function toPigLatin(ByVal textToTranslate As String) As String
  5. Our toPigLatin method of class pigLatin is going to accept a string of English text and return a string of Pig Latin text.
  6. Add Imports System.Web.Services and System.Text( for the StringBuilder class).
  7. Your Web Service page should look similar to this code.

Test Web Service

  1. Right click the pigLatin.asmx page and choose "Select as start page". Press F5
  2. A link with our toPigLatin method appears.
  3. Click the link. A test page appears asking for a parameter of textToTranslate. Hey that is the parameter for my toPigLatin method. Take a look at the URL the method is appended. "Neat stuff".
  4. Press the invoke button. An XML page with the return value from our method is shown. This can be used by the Web Service client in their application.

Register Service 

  1. Right click on references in the Solution Explorer
  2. Select "Add Web Reference"
  3. You will see a Web Directory page. In the address textbox type in the full address of the pigLatin.asmx page.
  4. Click the "add reference" button. 
    OR if you are connected to the Internet enter the following
    http://www.aspxpressway.com/maincontent/webservices/piglatin.asmx
  5. In the Solution Explorer under Web References you will see the Service listed.

Consume Service

  1. Add a new Web Form called translate.aspx
  2. Create an object from the Registered Web Service  as follows:
    dim objTranslate as new localhost.pigLatin
    or
    dim objTranslate as new com.aspxpressway.www.pigLatin
  3. Create a text box called txtInputText, a label called lblPigLatin and a button called btnSubmit
  4. Double click on the button and add the following code:
    Dim txtInputText As String = txtEnglish.Text
    lblPigLatin.Text = objPiglatin.toPigLatin(txtInputText)
  5. Right-click the translate.aspx page and choose "Select as start page". Press F5 to run.
Opehay atthay you can anslatetray English-hay to Pig Atinlay now!


Marketplace
(Sponsored Links)
What are the green links?
   



 
Copyright © 2007 CMP Tech LLC |
Privacy Policy (4/10/06) | Your California Privacy Rights (4/10/06) | Terms of Service | Advertising Info | About Us | Help