10.02.2014 Views

Beginning Ajax With ASP.NET (2006).pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10<br />

<br />

<br />

New Time:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

In the preceding code, on a button click, two remote calls are made out to the server — a call to the<br />

GetServerCachedTime method and one to the GetServerCurrentTime method. The GetServer<br />

CachedTime method on the server returns the current server time when it is called the first time. The<br />

result of this call is cached in server memory. Subsequent calls are returned the cached result for the next<br />

100 seconds, at which point the next request will generate a new time and a new entry in the server cache.<br />

As a result, calls to GetServercurrentTime() will result in the current time of the server being<br />

returned while calls to GetServerCachedTime() will result in a value that is up to 100 seconds old.<br />

The following is the code for a web service:<br />

[WebMethod(CacheDuration = 100)]<br />

public DateTime GetServerCachedTime()<br />

{<br />

return(DateTime.Now.ToUniversalTime());<br />

}<br />

[WebMethod]<br />

public DateTime GetServerCurrentTime()<br />

{<br />

return (DateTime.Now.ToUniversalTime());<br />

}<br />

Atlas uses UTC encoding for dates and time. This may create confusion for you if you don’t remember<br />

it. Personally, my system is set with the time of GMT – 5. As a result, the times that were coming back<br />

were 5 hours off.<br />

Figure 10-7 shows the results of multiple calls to the cached and noncached web services.<br />

In Figure 10-7, you will notice the two different time values. This shows that calling the cached web service<br />

returns the cached value and the noncached version returns the current value of the DateTime.Now<br />

object.<br />

264

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!