Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


XML Web Services: Just Another Data Tier? : Page 3

Many developers build a Web site and later, add XML Web services as separate projects, resulting in two code bases. This article shows you how to reuse XML Web service to provide data to your own applications.

Caching Web Services
In addition to the simplistic HelloWorld Web service, the class exposes a second Web service called ListCurrencies, which loads the XML file currency.xml into a DataSet, and returns the result. Here's the method code:

<WebMethod()> _ Public Function ListCurrencies() As DataSet Dim ds As New DataSet("CurrencyList") ds.ReadXml(Server.MapPath("currency.xml")) Return ds End Function

In the section of the Web Form marked "Example 2", the Button2_click event handler code creates an instance of the Web service as an ordinary class, and sets the DataSource property of the control Dropdownlist1 to the result of the ListCurrencies method. So when you click the "ListCurrencies" button the page calls the ListCurrencies method and populates the dropdown list (see Figure 3).

Figure 3: When you click the "ListCurrencies" button, the page calls the ListCurrencies method and populates the drop-down list.
Because any code that you run repeatedly consumes server resources, always consider caching the output when possible. The output of the ListCurrenciesCached method exposed by the api class illustrates how you can cache Web services. The api class inherits System.Web.Services.WebService, so it automatically "knows" about the current Context. The HttpContext class exposes a Cache property that returns an instance of a useful, high performance caching class that can hold objects in memory and retrieve them using simple name-value pairs—just as you use the HttpApplication object to store global data such as database connection strings or mail server information.

The difference between using the Cache object and the Application object is that you can tell the HttpContext.Cache to invalidate specific cached items on a regular schedule, at a specific time, or whenever a file changes. The code below checks to see if there is an object already in the cache with the key "CurrencyList". If there is, the code casts the cached object back to a DataSet and returns it. If the object isn't in the cache, the code calls the ListCurrencies method to obtain the DataSet, and then places the result in the cache using a file dependency. If any process updates the file while the data is cached, the cache flushes the object during the next request, and replaces it with the new data.

Public Function listCurrenciesCached() As DataSet If Not Context.Cache("CurrencyList") Is Nothing Then Return CType(Context.Cache("CurrencyList"), _ DataSet) Else Dim CurFile As String = _ Server.MapPath("currency.xml") Dim ds As New DataSet() ds = ListCurrencies() Context.Cache.Insert("CurrencyList", ds, _ New System.Web.Caching.CacheDependency _ (CurFile), _ Now.AddHours(1), Nothing) Return ds End If

The Web service is the "right" tier in which to do this caching—it is on the web server, in front of the Business Logic and Data Access code, but still available to any web page / User Interface that wants to use it.

Although this example is trivial the concept applies equally well to more complicated scenarios. Consider a case where you want to provide such a list based on the contents of a database table whose contents may change on a daily basis as new product lines are added. You would not want to query the database every time someone visits the page. You could arrange for the database to write an xml file whenever someone updates the table (The SQL Server web wizard makes this trivial) and then the above method would always present up-to date information, while the load on the database is hugely reduced.

For a shop site, while you would probably not put your entire product catalog into the cache, you might cache a subset of the product tables, perhaps "Department," "Aisle," and "Shelf," and then select from the database depending on the cached "DepartmentID," "AisleID," and "ShelfID" values.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date