"Asynchronous" execution means that functions that were originally executed from start to finish are truncated in the bottom and divided into two different functions. The relationship between these two functions is often very close, for example, sometimes we need to access the variables computed in the previous function in the latter function (that is, the callback function), or to get information such as the context in which the previous function was executed in the latter function. In particular, if several different asynchronous functions provide the same callback function for processing, we generally know which function in the callback function raised this callback. In order to satisfy these requirements, the concept of user context is naturally generated in asynchronous invocation.
The asynchronous invocation model provided by the ASP.net Ajax Asynchronous communication layer also provides support for passing user context information. We still use a simple sample program to demonstrate its specific implementation.
The function of the program is not complicated: All two buttons will asynchronously invoke the same Web service on the server side and specify the same callback function, which is displayed after the server-side current time, and also displays information about which button the user clicked. As shown in Figure 3-7 and figure 3-8, note the differences in the user context.
Figure 3-7 Click on the first button to get the server-side current time
Figure 3-8 Click on the second button to get the server-side current time
The WEB service is declared as follows, returning the current server-side time:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class UserContextService : System.Web.Services.WebService
{
[WebMethod]
public string GetServerDateTime()
{
return DateTime.Now.ToString();
}
}