Chapter 7: Maintaining State and Sequencing Operations


image from book Download CD Content

Overview

After completing this chapter, you will be able to:

  • Describe how WCF creates an instance of a service.

  • Explain the different options available for creating service instances.

  • Manage state information in a WCF service in a scalable manner.

  • Fine-tune the way in which the WCF runtime manages service instances.

  • Describe how to control the life cycle of a service instance.

In all the exercises that you have performed so far, the client application has invoked a series of operations in a WCF service. The order of these operations has been immaterial, and calling one operation before another has had no impact on the functionality of either; the operations are totally independent. In the real world, a Web service might require that operations be invoked in a particular sequence. For example, if you are implementing shopping cart functionality in a service, it does not make sense to allow a client application to perform a checkout operation to pay for goods before actually putting anything into the shopping cart.

The issue of sequencing operations should naturally lead you to consider the need to maintain state information between operations. Taking the shopping cart example, where should the data that describes the items in the shopping cart be held? You have at least two options:

  • Maintain the shopping cart in the client application, pass the information that describes the shopping cart contents as a parameter to each operation, and return the updated shopping cart contents from the operation back to the client. This is a variation of the solution implemented by traditional Web applications (including ASP.NET Web applications) that used cookies stored on the user’s computer to store information. It relieved the Web application of the burden of maintaining state information between client calls, but there was nothing to stop the client application directly modifying the data in the cookie or even inadvertently corrupting it in some manner. Additionally, cookies can be a security risk, so many Web browsers implement features to enable a user to disable their use, making it difficult to store state information on the user’s computer. In a Web service environment (as opposed to a Web application and browser combination), a client application can maintain state information by using its own code rather than relying on cookies. However, this strategy ties the client application to the Web service and can result in a very tight coupling between the two, with all the inherent fragility and maintenance problems that this can cause.

  • Maintain the shopping cart in the service. The first time the user running the client application attempts to add something to the shopping cart, the service creates a data structure to represent the items being added. As the user adds further items to the shopping cart, they are stored in this data structure. When the user wants to pay for the items in the shopping cart, the service can calculate the total, perform an exchange with the user through the client application to establish the payment method, and then arrange for dispatch of the items. In a WCF environment, all interactions between the client application and the service are performed by invoking well-defined operations specified by using a service contract. Additionally, the client application does not need to know how the service actually implements the shopping cart.

The second approach sounds the more promising of the two, but there are several issues that you must address when building a Web service to handle this scenario. In this chapter, you will investigate some of these issues, and see how you can resolve them.




Microsoft Windows Communication Foundation Step by Step
Microsoft Windows Communication Foundation Step by Step (Step By Step Developer Series)
ISBN: 0735623368
EAN: 2147483647
Year: 2007
Pages: 105
Authors: John Sharp

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