Examining XML Web Service and Web Site Interaction


An ASP.NET Web service and a Web site can happily coexist in the same application space. In particular, a Web service and Web site can share the same application and session state. In the following sections, you learn how to use application and session state with Web services.

XML Web Services and Application State

A Web site and Web service can share the same application state. You can use application state as a mechanism to exchange information between a Web site and a Web service.

For example, the Web service in Listing 22.15 can be used as a simple counter. Every time you call the GetAppCounter method, an application item named Counter is incremented by one. Because Counter is maintained in application state, the value of the Counter item is not lost between requests to the Web service.

NOTE

To learn more details about application state, see Chapter 15, "Creating ASP.NET Applications."


Listing 22.15 ApplicationService.asmx
 <%@ WebService Class="ApplicationService" %> Imports System Imports System.Web.Services <WebService( Namespace:="http://yourdomain.com/webservices" )> _ Public Class ApplicationService : Inherits WebService <WebMethod()>  Public Function GetAppCounter() As Integer   Application( "Counter" ) = CINT( Application( "Counter" ) ) + 1   Return CINT( Application( "Counter" ) ) End Function End Class 

The C# version of this code can be found on the CD-ROM.

After you create the ApplicationService Web service in Listing 22.15, you can use it in an ASP.NET page by creating a proxy class. First, you must use the Wsdl.exe tool to create the source file for the proxy class like this:

 
 Wsdl /l:vb http://www.SomeSite.com/ApplicationService.asmx?wsdl 

Next, you need to compile the proxy class like this:

 
 vbc /t:library /r:System.dll,System.Web.Services.dll,System.XML.dll ApplicationService.vb 

After you copy the compiled proxy class to the application's /bin directory, you can use the class in an ASP.NET page. The page in Listing 22.16 uses the ApplicationService Web service to track the number of times the page has been requested .

Listing 22.16 ApplicationCounter.aspx
 <Script Runat="Server"> Sub Page_Load   Dim objApplicationService As ApplicationService   objApplicationService = New ApplicationService   lblCounter.Text = objApplicationService.GetAppCounter() End Sub </Script> <html> <head><title>ApplicationCounter.aspx</title></head> <body> This page has been requested <asp:Label   ID="lblCounter"   Runat="Server" /> times! </body> </html> 

The C# version of this code can be found on the CD-ROM.

In Listing 22.16, an instance of the ApplicationService proxy class is initialized in the Page_Load subroutine. Next, the GetAppCounter method is called to get the current value of the counter stored in application state. The value returned by the method is assigned to a Label control.

XML Web Services and Session State

Using session state with Web services is a little trickier than using application state.

By default, session state is disabled for Web service methods for performance reasons (maintaining session state requires some extra work on the part of the server). However, if you want, you can modify the EnableSession property of the WebMethod attribute to enable session state for a particular method.

NOTE

To learn more information about session state, see Chapter 16, "Tracking User Sessions."


The page in Listing 22.17, for example, takes advantage of session state to save the value of a counter between requests to the Web service.

Listing 22.17 SessionService.asmx
 <%@ WebService Class="SessionService" %> Imports System Imports System.Web.Services <WebService( Namespace:="http://yourdomain.com/webservices" )> _ Public Class SessionService : Inherits WebService <WebMethod( EnableSession:=True )> Public Function GetSessionCounter() As Integer   Session( "Counter" ) = CINT( Session( "Counter" ) ) + 1   Return CINT( Session( "Counter" ) ) End Function <WebMethod( EnableSession:=True )> Public Function GetSessionID() As String   Return Session.SessionID End Function End Class 

The C# version of this code can be found on the CD-ROM.

Notice that the EnableSession property is set to the value True within the WebMethod attribute for both the GetSessionCounter() and GetSessionID() methods. If you neglect to enable this property and you attempt to access an item from session state, you receive a Null Reference error.

After you create the SessionState Web service, you can test it by opening the SessionService.asmx page in your browser. If you call the GetSessionCounter() method, the session counter should increment as you expect. If you call the GetSessionID() method, the same session ID should be displayed each time the method is called.

The Web service will work fine when you call its methods from a Web browser. You'll encounter problems, however, when you call its methods from a proxy class. Go ahead and try it.

Generate a proxy class for the SessionState Web service by executing the following statement from a command prompt:

 
 wsdl /l:vb http://www.SomeSite.com/SessionService.asmx?wsdl 

Next, compile the source code for the proxy class like this:

 
 vbc /t:library /r:System.dll,System.Web.Services.dll,System.XML.dll SessionService.vb 

Finally, copy the compiled class, SessionService.dll , to your application's /bin directory.

The page in Listing 22.18 calls the new SessionService proxy to display the value of the session counter and session ID.

Listing 22.18 SessionCounterBad.aspx
 <Script Runat="Server"> Sub Page_Load   Dim objSessionService As SessionService   objSessionService = New SessionService   lblSessionCounter.Text = objSessionService.GetSessionCounter()   lblSessionID.Text = objSessionService.GetSessionID() End Sub </Script> <html> <head><title>SessionCounterBad.aspx</title></head> <body> <h2>This page does not work!</h2> Your session ID is <asp:Label   ID="lblSessionID"   Runat="Server" /> <p> This page has been requested by you <asp:Label   ID="lblSessionCounter"   Runat="Server" /> times! </body> </html> 

The C# version of this code can be found on the CD-ROM.

Each time you request the page in Listing 22.18, a new session ID is displayed. The session counter will always have the value 1. The problem is the Web service will interpret each call by the proxy class as the start of a new session.

Remember that session state relies on a cookie to identify requests within the same session. A special cookie named ASP.NET_SessionId must be passed to the server with every request to identify the user. Since, by default, this cookie won't be passed by the proxy class, session state won't work.

If you want your proxy class to be able to take advantage of session state, you have to do some extra work. You need to explicitly pass the same ASP.NET_SessionId cookie to the Web service proxy with each request. This is illustrated in Listing 22.19.

Listing 22.19 SessionCounterGood.aspx
 <%@ Import Namespace="System.Net" %> <Script Runat="Server"> Sub Page_Load   Dim objSessionService As SessionService   objSessionService = New SessionService   objSessionService.CookieContainer = New CookieContainer   If Session( "CookieContainer" ) Is Nothing Then     Session( "CookieContainer" ) = objSessionService.CookieContainer   Else     objSessionService.CookieContainer = Session( "CookieContainer" )   End If   lblSessionCounter.Text = objSessionService.GetSessionCounter()   lblSessionID.Text = objSessionService.GetSessionID() End Sub </Script> <html> <head><title>SessionCounterGood.aspx</title></head> <body> <h2>This page works!</h2> Your session ID is <asp:Label   ID="lblSessionID"   Runat="Server" /> <p> This page has been requested by you <asp:Label   ID="lblSessionCounter"   Runat="Server" /> times! </body> </html> 

The C# version of this code can be found on the CD-ROM.

In Listing 22.19, you explicitly create an instance of the CookieContainer class. This class contains a collection of all the cookies associated with each Web service request. One of these cookies is the ASP.NET_SessionId cookie, which is used to maintain session state.

The CookieContainer class is saved in session state between requests. By saving the CookieContainer in session state, you can ensure that the same value for the ASP.NET_SessionId cookie is passed with the Web method request each time the same user requests the page.

If you request the SessionCounterGood.aspx page, the session counter should increment as expected. You should also notice that the session ID displayed by the page remains constant (see Figure 22.8).

Figure 22.8. The SessionCounterGood.aspx page.

graphics/22fig08.jpg



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

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