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
- Create a new ASP.NET Web Application using VB.NET
-
Select File > New Item > Web Service
- Name pigLatin.asmx and open up the code view page
-
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
- Our toPigLatin method of class pigLatin
is going to accept a string of English text and return a string of Pig
Latin text.
- Add Imports System.Web.Services and
System.Text( for the StringBuilder
class).
-
Your Web Service page should look similar to this code.
Test Web Service
- Right click the pigLatin.asmx page and choose "Select
as start page". Press F5
- A link with our toPigLatin method
appears.
- 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".
-
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
-
Right click on references in the Solution Explorer
- Select "Add Web Reference"
- You will see a Web Directory page. In the address textbox type in
the full address of the pigLatin.asmx page.
- Click the "add reference" button.
OR if you are connected to the Internet enter the following
http://www.aspxpressway.com/maincontent/webservices/piglatin.asmx
-
In the Solution Explorer under Web References you will see the Service listed.
Consume Service
- Add a new Web Form called translate.aspx
-
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
- Create a text box called txtInputText,
a label called lblPigLatin and a button
called btnSubmit
-
Double click on the button and add the following code:
Dim txtInputText As String = txtEnglish.Text
lblPigLatin.Text = objPiglatin.toPigLatin(txtInputText)
- 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!