Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to perform output caching with Web services in Visual C# .NET


View products that this article applies to.

Summary

Note The following .NET Framework Class Library namespaces are referenced in this article:
  • System.Web.Services
  • System.Text
This step-by-step article describes how to create a sample ASP.NET Web service that uses output caching. Output caching caches the output response result of a Web service based on the Duration attribute that is specified for a corresponding WebMethod.

Note In ASP.NET 2.0, the HTTP method of the test page has changed from GET to POST. However, POSTs are not ordinarily cached. If you change the test page in an ASP.NET 2.0 Web service application to use GET, caching works correctly. Additionally, HTTP indicates that a user agent (the browser or calling application) should be able to override server caching by setting the Cache-Control header to "no-cache." Therefore, ASP.NET applications ignore cached results when they find a "no-cache" header.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows XP
  • Microsoft .NET Framework
  • Microsoft Internet Information Services (IIS)

Create a New ASP.NET Web Service application

Create a new ASP.NET Web Service application named WSCacheSample:
  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project to start the New Project Wizard.
  3. Under Project types, select Visual C#. Under Template, select ASP.NET Web Service.
  4. In the Location box, replace "WebService#" in the URL path with the name of your project, WSCacheSample. If you are using the local server, leave the server name as http://localhost, so that the Location box looks similar to this:
    http://localhost/WSCacheSample

Create the sample Web service

  1. In Solution Explorer, right-click the project node, point to Add, and then click Add Web Service.
  2. For the name, type CacheDemo.asmx, and then click Open. The Web service opens in Design view.
  3. Right-click the Web service, and then click View Code.
  4. Add the following code to the CacheDemo.asmx.cs class file. This adds a WebMethod attribute named GetCacheEntryTime with a CacheDuration of 60 seconds.
    [WebMethod(CacheDuration=60)]
    public string GetCacheEntryTime(string Name)
    {
    	StringBuilder sb = new StringBuilder("Hi ");
    	sb.Append(Name);
    	sb.Append(", the Cache entry was made at ");
    	sb.Append(System.DateTime.Now.ToString());
    	
    	return(sb.ToString());
    }
    						
    NOTE: By default, the CacheDuration for a WebMethod attribute is set to 0, meaning that it is not cached.
  5. Because the sample code uses the StringBuilder method, include a reference to the System.Text namespace. The namespace listing for the Web service looks similar to this:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    using System.Text;
    						
    NOTE: For more information about the StringBuilder class, visit the following Microsoft Developer Network (MSDN) Web site:
  6. In the Visual Studio .NET IDE, click Build Solution on the Build menu.
  7. On the File menu, click Save All to save the project and the associated files.

Test the project

Now that you have created the sample CacheDemo Web service, run the GetCacheEntryTimeWebMethod to see the effects of the CacheDuration attribute setting:
  1. In Solution Explorer, right-click CacheDemo.asmx, and then click View in browser.

    The .asmx file opens in the browser, and the GetCacheEntryTimeWebMethod attribute is listed as a bulleted item at the top of the page.
  2. Click the GetCacheEntryTime link.
  3. In the Name box for the method, type Joe and then click Invoke to run the WebMethod attribute and return the XML result. Notice the time stamp that is returned in the message.

    NOTE: If the WebServices help page does not appear, you can run the WebService method by typing the following in the address box in the Web browser:
    http://localhost/wscachesample/cachedemo.asmx/GetCacheEntryTime?Name=Joe
    					
  4. Run the WebMethod again by typing Joe. NOTE: If you run the WebMethod attribute in the 60 second time period that is specified by the CacheDuration attribute, the same time stamp appears.
  5. Repeat step 4, but type Amy instead of Joe in the Name box for the WebMethod attribute parameter.

    Notice that the time stamp result is different. This occurs because the default output caching result is based on the parameters of the WebMethod attribute. In this example, Joe is the parameter value for the first two tests and the cached output is returned for the second test. When you use Amy in the third test, you receive a new result. If you repeat the test in 60 seconds, you will receive a cached output result. The difference in the output caching version is related to the parameter of the WebMethod attribute.

Troubleshooting

When you decide whether or not to implement output caching for your Web service, remember that server resources can be affected negatively if the WebMethod attribute parameters that are associated with the requests vary widely or if the responses involve large amounts of data.

↑ Back to the top


References

For more information about the WebMethodAttribute.CacheDuration property and the WebMethodAttribute class, see the following topic in the .NET Framework Class Library documentation: For additional information about Web services, visit the following MSDN Web sites:
Design Guidelines for XML Web Services Created Using ASP.NET
http://msdn2.microsoft.com/en-us/library/w8excbb0(vs.71).aspx

Using the WebMethod Attribute
http://msdn2.microsoft.com/en-us/library/byxd99hx(vs.71).aspx
For additional samples, documentation, and links that are related to programming with the .NET Framework, visit the following CodePlex Web site:

↑ Back to the top


Keywords: KB318299, kbhowtomaster, kbcaching

↑ Back to the top

Article Info
Article ID : 318299
Revision : 11
Created on : 8/28/2007
Published on : 8/28/2007
Exists online : False
Views : 1052