Caching in Web Services

only for RuBoard

Inherently, Web services support output caching by use of the CacheDuration property of the WebMethodAttribute class. The CacheDuration WebMethodAttribute is similar to the @OuputCache Duration attribute in that you set its value equal to the number of seconds that a response should remain in the cache. In Listing 16.9 I'll demonstrate how to enable output caching for a WebMethod that returns a DataSet .

Listing 16.19 illustrates how to cache the output of a Web service which returns a DataSet object.

Listing 16.19 Implementing Output Caching in the GetProducts Method
 [VisualBasic.NET] 01: <%@ WebService Language="vb" Class="ProductServices" %> 02: 03: imports System.Data 04: imports System.Data.SqlClient 05: imports System.Web.Services 06: 07:  public class ProductServices : inherits WebService 08: 09:   <WebMethod(CacheDuration := 60)> _ 10:   Public Function GetProducts(RecordCount As Integer) As DataSet 11:   _ 12:       dim ProductsDataSet ad new DataSet() 13:       dim sCon as new SqlConnection("server=localhost;" & _ 14:       "uid=sa;pwd=;database=northwind") 15:       dim SqlCmd as string = "SELECT TOP " & RecordCount & " * FROM Suppliers" 16:       dim sda as new SqlDataAdapter(SqlCmd,sCon) 17: 18:       sda.Fill(ProductsDataSet,"Products") 19: 20: 21:       return ProductsDataSet 22:   end function 23: end class [C#.NET] 01: <%@ WebService Language="c#" Class="ProductServices" %> 02: 03: using System.Data; 04: using System.Data.SqlClient; 05: using System.Web.Services; 06: 07: public class ProductServices : WebService { 08: 09:  [ WebMethod(CacheDuration=10) ] 10:      public DataSet GetProducts(int RecordCount) { 11: 12:       DataSet ProductsDataSet = new DataSet(); 13:       SqlConnection sCon = new SqlConnection("server=localhost;" + 14:       "uid=sa;pwd=;database=northwind"); 15:       string SqlCmd = "SELECT TOP " + RecordCount + " * FROM Suppliers"; 16:       SqlDataAdapter sda = new SqlDataAdapter(SqlCmd,sCon); 17: 18:       sda.Fill(ProductsDataSet,"Products"); 19: 20: 21:       return ProductsDataSet; 22:      } 23: } 

Listing 16.19 illustrates a very simple example of how to implement output caching within a Web service. It is actually a very complex process, but, with my superb programming skills, I was able to simplify it okay, it is this easy. Simply use the CacheDuration WebMethod Attribute , and assign an amount in seconds. In this example, I cached the output for 10 seconds.

Before you invoke the GetProducts Web method, you must enter an integer value equal to the number of rows you want returned in the DataSet from the Products table. This integer value will be the value by which the cache varies. Go through and invoke the method using 2 for the value, and then 4 . Make a mental note of the approximate length of time the response takes. Now go through and do it again. You will notice that the response is quicker. This is because the response is now being served from the output cache.

In addition to output caching, you also can take advantage of data caching in Web services. However, you need to access it a little differently than in output caching. First, you have to include the System.Web namespace within your Web service. The System.Web namespace contains all the classes and interfaces that enable browser and server communication. Specifically, it contains information about the current HTTP request object. This is what you use to access the Web applications' cache. Listing 16.20 presents a simple example of using data caching within a Web service. The example has one GetString Web method that puts a string into the cache and then returns the string from the cache.

Listing 16.20 Using Data Caching Within a Web Service
 [VisualBasic.NET] 01: <%@ WebService Language="vb" Class="StringService" %> 02: 03: imports System.Web.Services 04: imports System.Web 05: 06: public class StringService : inherits WebService 07: 08:     <WebMethod()> _ 09:     Public Function _GetString() As string 10: 11:       dim MyString as string ="The Mack is back!" 12:       HttpContext.Current.Cache.Insert("MyStringCache", MyString) 13: 14:       return CType(HttpContext.Current.Cache.Get("MyStringCache"), string) 15: 16:   end function 17: end class [C#.NET] 01: <%@ WebService Language="c#" Class="StringService" %> 02: 03: using System.Data; 04: using System.Data.SqlClient; 05: using System.Web.Services; 06: using System.Web; 07: 08: public class StringService : WebService { 09: 10:  [ WebMethod() ] 11:      public string GetString() { 12: 13:       string MyString ="The Mack is back!"; 14:       HttpContext.Current.Cache.Insert("MyStringCache", MyString); 15: 16:       return (string) HttpContext.Current.Cache.Get("MyStringCache"); 17: 18:      } 19: } 

In Listing 16.20, the applications cache is accessed through HttpContext.Current (the HttpContext object for the current HTTP Request) Cache property (lines 14 and 16). After you have a handle on the Cache object for the current Web application, you just need to call its respective methods ( Insert and Get ) to work with it. Figure 16.11 contains an image of this Web service after the GetString Web method has been invoked.

Figure 16.11. "The Mack is Back!" is returned by the Web method. This string was returned by the Web method directly from the cache.
only for RuBoard

Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170 © 2008-2017.
If you may any questions please contact us: