12.3 Use Data Caching in an XML Web Service


Problem

You need to run your Web method code but still use some cached information. Or, you want to improve XML Web service performance by caching some data, but you need to reuse cached data among several methods .

Solution

You can store any object in the cache using the Insert method of the System.Web.Caching.Cache object. You can access the cache through the static HttpContext.Current property.

Discussion

Data caching works almost the same with an XML Web service as it does with a Web page. (You can even store data in the cache using Web page code and retrieve it in an XML Web service, or vice versa.) For more information about data caching and the different types of expiration policies it supports, refer to recipe 7.15.

The only difference between caching in an XML Web service and caching in a Web page is that in the former case, you can't retrieve the Cache object as a built-in property. Instead, you need to access the cache through the static HttpContext.Current property.

The following example shows an XML Web service with two Web methods. GetProductCatalog returns a DataSet with a table of product information. This DataSet is either retrieved from the cache or generated automatically (if required) using the private GetProductDataSet function. The second Web method, GetProductList , also uses the customer DataSet and the GetProductDataSet function. However, it retrieves a subset of the available information just the product names and returns it as an array of strings. The end result is that both Web methods can use the same cached data, reducing the burden that is placed on the database.

The full XML Web service code is shown here:

 using System; using System.Data; using System.Data.SqlClient; using System.Web.Services; using System.Web; public class DataCachingTest {     private static string connectionString = "Data Source=localhost;" +       "Initial Catalog=Northwind;user ID=sa";     [WebMethod()]      public DataSet GetProductCatalog() {         // Return the complete DataSet (from the cache, if possible).         return GetCustomerDataSet();     }     [WebMethod()]     public string[] GetProductList() {         // Get the customer table (from the cache if possible).         DataTable dt = GetCustomerDataSet().Tables[0];                      // Create an array that will hold the name of each customer.         string[] names = new string[dt.Rows.Count];         // Fill the array.         int i = 0;         foreach (DataRow row in dt.Rows) {             names[i] = row["ProductName"].ToString();             i += 1;         }         return names;     }     private DataSet GetCustomerDataSet() {         // Check for cached item.         DataSet ds = HttpContext.Current.Cache["Products"] as DataSet;         if (ds == null) {             // Recreate the item.             string SQL = "SELECT * FROM Products";             // Create ADO.NET objects.             SqlConnection con = new SqlConnection(connectionString);             SqlCommand com = new SqlCommand(SQL, con);             SqlDataAdapter adapter = new SqlDataAdapter(com);             ds = new DataSet();             // Execute the command.             try {                 con.Open();                 adapter.Fill(ds, "Products");                 // Store the item in the cache (for 60 seconds).                 HttpContext.Current.Cache.Insert("Products", ds, null,                   DateTime.Now.AddSeconds(60), TimeSpan.Zero);             } catch (Exception err) {                 System.Diagnostics.Debug.WriteLine(err.ToString());             } finally {                 con.Close();             }         }         return ds;     } } 

You can test this example, and examine how often the cache is being used, by setting breakpoints with the Visual Studio .NET debugger. You'll also need to be running SQL Server or MSDE.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

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