Revisiting Session Objects

Revisiting Session Objects

As discussed, before a web service can support session-variable operations, the client program must support cookie operations. After a web service creates a session, the service will return the session’s ID to the client within a cookie. Before the service can return the information to the client, the client must provide service with a Cookie collection into which the service can assign the unique session identifier.

The following SessionId web service illustrates the processing a web service must perform to create and return a unique session ID. The service supports one method, SessionCount, which returns an integer value that corresponds to the number of times the current program has called the web service:

Integer SessionCount()

To create the SessionId web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name SessionId. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 5.14.

Listing 5.14 SessionId.asmx.vb

start example
<WebMethod(EnableSession:=True)> Public Function SessionCount() As Integer     Dim Counter As Integer     If (Session.Item("UseCount") Is Nothing) Then         Session.Add("UseCount", "1")         Counter = 1     Else         Counter = CType(Session.Item("UseCount"), Integer)         Counter = Counter + 1         Session.Item("UseCount") = Counter     End If     SessionCount = Counter End Function
end example

Note the change to the <WebMethod> entry in the first line of the code. Before a web service can support Session operations, you must enable session support on a per-method basis by placing the EnableSession:=True attribute setting within the <WebMethod> entry, as shown here:

<WebMethod(EnableSession:=True)>

The following C# program, ShowSessionCount.cs, uses the SessionId web service to display the number of times the current instance of the program (the current session) has called the service, as shown in Figure 5.9. If you run the program within several different windows at the same time, each program will display its own session-specific data.

To create the ShowSessionCount.cs program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click C# Projects. Then, within the Templates field, click Windows Application. Within the Name and Location fields, type ShowSessionCount. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls.


    Figure 5.9: Displaying the session-based information returned to a client

  3. Using the Toolbox, drag and drop the text boxes, labels, and buttons previously shown in Figure 5.9 onto the form.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/SessionId/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 5.15.

Listing 5.15 ShowSessionCount.cs

start example
System.Net.CookieContainer Cookies = new System.Net.CookieContainer(); private void button1_Click(object sender, System.EventArgs e) {    localhost.Service1 WS = new localhost.Service1();    try    {       WS.CookieContainer = Cookies;       textBox1.Text = WS.SessionCount().ToString();    }    catch (Exception Ex)    {       textBox1.Text = Ex.Message;    } }
end example

As you can see, the program code creates a cookie container as a global variable, so the container’s contents will be available each time the button1_Click method executes. Within the method, the program assigns the cookie container to the web service object and then uses the web service. The remote service, in turn, will use and update the container’s contents each time the program calls the method.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net