[EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] wrote:

> I am using the WebMethodAttribute.CacheDuration Property.  I
> was wondering if the client or the web service itself can
> tell where the dataset was comming from.

Ahh... ok. The client can't tell no matter how you cache it unless you
return an extra piece of data saying so. The web service itself can't tell
with the WebMethodAttribute::CacheDuration property set because the ASP.NET
web service plumbing will never make a call to the web method unless it
detects that the item is no longer in the cache. It's a great feature, but
it sounds like you need more control. In your case, you should handle
caching within the web method yourself. You can tell if the item is cached
by first trying to fetch it from the cache. If the result is null, then the
item no longer exists in the cache and you must rebuild it (cache miss) and
Insert/Add it back into the cache. Here's some sample code:

<codeSnippet language="C#">
public class MyWebService : WebService
{
  [WebMethod]
  public int GetSomeData()
  {
    // Look for the cached data
    object cachedResult = base.Cache["myCachedData"];
    int result;

    // Data never/no longer existed in cache, calculate and cache now
    if(cachedResult == null)
    {
      Trace.WriteLine("Cache miss!")

      result = this.InternalGetSomeData();

        // Cache with no dependencies with an expiration of 30 minutes from
now
      base.Cache.Insert("myCachedData", result, null,
DateTime.Now.AddMinutes(30), TimeSpan.Zero);
    }
    else
    {
      Trace.WriteLine("Cache hit!")

        // The data existed in the cache, just use that value
      result = (int)cachedResult;
    }

    return result;
  }

  private int InternalGetSomeData()
  {
        ... do whatever to get the data ...
  }
</codeSnippet>

HTH,
Drew
.NET MVP

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to