Export (0) Print
Expand All

WebMethodAttribute.EnableSession Property

Indicates whether session state is enabled for an XML Web service method.

Namespace:  System.Web.Services
Assembly:  System.Web.Services (in System.Web.Services.dll)

public bool EnableSession { get; set; }

Property Value

Type: System.Boolean
true if session state is enabled for an XML Web service method. The default is false.

In order to store session state in the ASP.NET HttpSessionState object, the XML Web service must inherit from WebService and a WebMethodAttribute applied to the XML Web service method, setting the EnableSession property to true. If session state is not needed for an XML Web service method, then disabling it may improve performance.

An XML Web service client is uniquely identified by an HTTP cookie returned by an XML Web service. In order for an XML Web service to maintain session state for a client, the client must persist the cookie. Clients can receive the HTTP cookie by creating a new instance of CookieContainer and assigning that to the CookieContainer property of the proxy class before calling the XML Web service method. If you need to maintain session state beyond when the proxy class instance goes out of scope, the client must persist the HTTP cookie between calls to the XML Web service. For instance, a Web Forms client can persist the HTTP cookie by saving the CookieContainer in its own session state. Because not all XML Web services use session state and thus clients are not always required to use the CookieContainer property of a client proxy, the documentation for the XML Web service should state whether session state is used.

The example below uses session state to determine how many times a particular session accesses the XML Web service method SessionHitCounter.

<%@ WebService Language="C#" Class="Util" %>

 using System.Web.Services;

 public class Util: WebService {
   [ WebMethod(Description="Per session Hit Counter",EnableSession=true)]
    public int SessionHitCounter() {
       if (Session["HitCounter"] == null) {
          Session["HitCounter"] = 1;
       }
       else {
          Session["HitCounter"] = ((int) Session["HitCounter"]) + 1;
          }
       return ((int) Session["HitCounter"]);
    }   
 }

The following code example is a Web Forms client of an XML Web service that uses session state. The client persists the HTTP cookie that uniquely identifies the session by storing it in the client's session state.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>

<html>

    <script runat="server">

        void EnterBtn_Click(Object Src, EventArgs E) 
	{
	  // Create a new instance of a proxy class for your XML Web service.
	  ServerUsage su = new ServerUsage();
          CookieContainer cookieJar;

	  // Check to see if the cookies have already been saved for this session. 
	  if (Session["CookieJar"] == null) 
	    cookieJar= new CookieContainer();
          else
	   cookieJar = (CookieContainer) Session["CookieJar"];

		// Assign the CookieContainer to the proxy class.
		su.CookieContainer = cookieJar;

	  // Invoke an XML Web service method that uses session state and thus cookies. 
	  int count = su.PerSessionServiceUsage();         

	  // Store the cookies received in the session state for future retrieval by this session.
	  Session["CookieJar"] = cookieJar;

          // Populate the text box with the results from the call to the XML Web service method.
          SessionCount.Text = count.ToString();  
        }

    </script>
    <body>
       <form runat=server ID="Form1">

             Click to bump up the Session Counter.
             <p>
             <asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
             <p>
             <asp:label id="SessionCount"  runat=server/>

       </form>
    </body>
</html>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft