NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

ASP+ Web Services State Management

ASP+ WebService classes are stateless, meaning that the class instance is instantiated and then torn down on each web request. Class state, the data values of fields and properties, are consequently not maintained across web requests. ASP+ Web Service developers must instead utilize explicit state mechanisms to persist data values between roundtrips on the server.

The following sections discuss two of these mechanisms, ASP+ Application State and ASP+ Session State, and demonstrate how they can be used within an ASP+ Web Service to maintain state on the server.

ASP+ Application State

ASP+ Application State provides a dictionary-like API that enables web developers to store and share values across an entire web application. ASP+ developers can access application state from ASP+ Web Form Pages, ASP+ Application Classes (Global.asax) as well as ASP+ Web Services.

Web Service developers gain access to application state by subclassing their web service class from the System.Web.Service.WebService base class. This base class exposes an Application property that provides access to application state values.

For example, the following web service exposes a single webmethod, UpdateAppCounter, that updates a global HitCounter variable within application state:

<%@ WebService Language=”C#” %>
using System.Web.Services;
public class Counter : WebService {
     [ WebMethod ]
     public void UpdateAppCounter() {

          if (Application[“HitCounter”] == null) {
              Application[“HitCounter”] = 0;
          }
          else {
              Application[“HitCounter”] = ((int) Application[“HitCounter”]) + 1;
          }
     }
}

ASP+ Session State

ASP+ Session State provides a dictionary-like API that enables web developers to store and share values across a web session. Unlike ASP+ Application State, Session State is not shared across all browsers, but is instead scoped only to a specific client instance Each client instance has its own session state.

WebService developers gain access to session state by subclassing their webservice class from the System.Web.Service.WebService base class. This base class exposes a Session property that provides access to session state values.

For example, the below web service exposes a single method, UpdateSessionCounter, that updates a session HitCounter variable within session state:

<%@ WebService Language=”C#” %>

using System.Web.Services;

public class Counter : WebService {

     [ WebMethod ]
     public void UpdateSessionCounter() {

          if (Session[“HitCounter”] == null) {
              Session[“HitCounter”] = 0;
          }
          else {
              Session[“HitCounter”] = ((int) Session[“HitCounter”]) + 1;
          }
     }
}

Session state is available by default to all webservice classes. ASP+ Web Service developers can disable session state on individual methods if they know that session state will not be accessed during that request, and they do not wish to pay the performance penalty required to fetch and load it. Developers disable session state for a method using a custom boolean property, enablesessionstate, on the WebMethod metadata attribute of a method.

For example, the below web service enables session state for the UpdateSessionCounter() method, but disables it for the UpdateAppCounter() equivalent (since it doesn’t use session state):

<%@ WebService Language=”C#” %>

using System.Web.Services;

public class Counter : WebService {

     [ WebMethod ]
     public void UpdateSessionCounter() {

          if (Session[“HitCounter”] == null) {
              Session[“HitCounter”] = 0;
          }
          else {
              Session[“HitCounter”] = ((int) Session[“HitCounter”]) + 1;
          }
     }

     [ WebMethod(EnableSessionState=false) ]
     public void UpdateAppCounter() {

          if (Application[“HitCounter”] == null) {
              Application[“HitCounter”] = 0;
          }
          else {
              Application[“HitCounter”] = ((int) Application[“HitCounter”]) + 1;
          }
     }
}