A Health Tip Web Service

   

My company has a number of medically related Web sites. One such Web site is www.YourPediatrician.com. This Web site specializes in health and prevention for babies and young children. One of the services we offer to the other Web sites is health tips that apply to young children. Before the .NET age, we offered these tips free of charge for other Web sites to embed using Java technology. I created a Java applet that queried an ASP on our server. The ASP on the server then decided which tip to serve up, depending on which day of the week it was. Then it returned HTML data to the Java applet. The Java applet would then parse to the HTML, get the tip data, and then display the tip in the Java applet. It was a lot of work on both sides, both server and client, to serve up these health tips.

What would have been better, because no user interface is involved, would have been to simply serve up a sentence or two containing the health tip. Then the client UI could decide what to do with it. Now with .NET we can do exactly that. On the server side I have created a Web Service. This Web Service gets requests , decides what tip needs to be sent out, then sends out the tip as a text stream. Absolutely no HTML or user interface accompanies the tip that is sent out. On the client side it is just a matter of calling the Web Service on the server to get the string that contains the health tip. No user interface is required, no fancy HTTP requests are required; just a simple call to a remote Web server is all it takes. Then the client-side code can take the health tip and embed it into the HTML in any way it seems fit.

You can see in Listing 14.1 the TipOfTheDay() method. This method has a WebMethod attribute. The WebMethod attribute tells Visual Studio.NET to automatically give it the code it needs to become a Web method. That means the code necessary to convert the method call to SOAP, and do the remote communications, is automatically added.

The first thing this method does is get the date. It converts the date into days by taking the current day and adding that to the month, multiplied by 30. It then makes sure that the number of days is not greater 32, because there are 33 tips numbered from 0 to 32. If the number happened to be greater than 32 there would be no case statement in which to assign the tip. Therefore, the values we get must be between 0 and 32 inclusive. Then the appropriate case statement assigns the tip data to the strTip variable. The strTip variable is then returned and because this is a Web method, the remote caller gets the data.

Listing 14.1 The Method that Returns the Health Tip
 [WebMethod]  public string TipOfTheDay()  {      DateTime dt = DateTime.Now;      int nMonth = dt.Month;      int nDay = dt.Day;      int nDays = nDay + nMonth * 30;      while( nDays > 32 )      {           nDays -= 32;      }      String strTip = "";      switch( nDays )      {          case 0:              strTip = "For toddlers, use a pea-sized amount of regular toothpaste to brush graphics/ccc.gif teeth. ";              break;          case 1:              strTip = "Stop using a bottle for your child to drink from at 12 MONTHS of graphics/ccc.gif age";              break;      . . .          case 31:              strTip = "NEVER allow toddlers or young children to be around a running lawn graphics/ccc.gif mower.";              break;          case 32:              strTip = "Get into a bedtime routine or ritual by 18 months of age. ";              break;      }      return( strTip );  } 

If you have ever written similar Web applications with classic ASP, you will appreciate how easy it was for me to write this "tip of the day" Web Service. Some simple code was all it took to create this method, and some simple code on the other end is all it takes to use it. (I'll talk about consuming Web Services shortly in the section entitled, "Consuming a Web Service.") If you have worked through creating this Web Service on your own, you can now go ahead and invoke it in the browser and see what kind of health tip it returns.

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net