Readme_ShoppingCart

The Shopping Cart sample uses the conversation group identifier to maintain state for a simple shopping cart application. The application uses the ServiceBrokerInterface sample.

#### Building and Installing the Sample

1. CD to the install directory and execute the following at a .NET Framework or Microsoft Visual Studio 2005 command prompt:

   `sn -k samplekey.snk`

2. In Visual Studio 2005, open `ShoppingCartCS.sln`.

3. Build the solution by pressing F6, or by selecting Build Solution from the Build menu.

4. Open a command prompt, locate the `Scripts` folder and run the following command:

   | Copy Code
   --- | ---
       install all

   The install script installs the SQL Server objects for the application, including the common language runtime stored procedure that implements the ShoppingCartService service.

#### Running the Sample

1. Run the `ShoppingCartClient.exe` application from the sample directory. For simplicity, the client processes one order each time the client runs.

2. Click the Create Order button to create an order. Add items to the shopping cart by selecting the item from the Item list, and then clicking the Add Item button.

3. Click the Service Broker Trace tab at any time to see a summary of the Service Broker messages sent and received.

4. The ShoppingList and StateTable tables in the ssb_ShoppingCart database maintain state for the application. The common language runtime stored procedure updates the tables based on messages from the client. You can use Management Studio or sqlcmd to inspect these tables.

#### Uninstalling the Sample

1. Open a command prompt, locate the `Scripts` folder and run the following command:

   | Copy Code
   --- | ---
       uninstall all

2. The uninstall script removes the SQL Server objects for the application.

# Requirements

This sample requires Visual Studio 2005. Because the sample uses features of the common language runtime that were not available in earlier versions, released versions of Visual Studio cannot build the sample.

# Demonstrates

Service Broker applications most often store state in the database that hosts the service. When messages arrive, the application loads state from the database, and then processes the messages.

The `Service` class of the Service Broker Interface sample provides a convenient way to maintain state. This sample demonstrates how to use the Service class to maintain state.

To maintain state using the `Service` class, you implement two methods in your application and a stored procedure in SQL Server, as listed in the following table:

###

Item | Signature | Description
--- | --- | ---
Stored Procedure | `CREATE PROCEDURE` name`(@cgid uniqueidentifier)` | Returns the state for the application. The result sets returned by this procedure must contain the conversation group identifier.
public method | `public override bool LoadState(System.Data.Sql.SqlReader reader)` | Reads the result sets provided by the stored procedure to restore state. This stored procedure must save the conversation group identifer in the class, since the Service Broker Interface does not provide the identifier to the `SaveState` method.
public method | `public override void SaveState()` | Saves state to the database.

This topic describes the general outline for restoring state. See `ShoppingCartService.cs` for the full example.

The stored procedure must accept a conversation group identifier, and return two result sets. The first result set consists of the conversation group identifier itself. The second result set contains the application-specific state. There is no fixed name for this stored procedure. Instead, you supply the name of the stored procedure by setting the `AppLoaderProcName` property; the Service Broker Interface simply calls the stored procedure provided.

The `LoadState` method restores the state for the application. The exact process the application follows depends on the state that the application requires. The method takes an instance of `System.Data.Sql.SqlReader` as a parameter. The `SqlReader` contains the result sets returned the stored procedure. Typically, implementations of the `LoadState` method update the `State` property of the `Service` class, save the conversation group identifier in a private field, and then save the application-specific state in appropriate data structures. For example, the `ShoppingCartService` stores information about items, quantities, and prices in a private instance of the `ShoppingCart` class.

After calling `LoadState`, the Service Broker Infrastructure dispatches the message. Notice that, if you use the attribute-based message dispatch framework, you can dispatch messages based on the State property as well as the message type name. In the methods that handle messages, you can read or update the data structures created in `LoadState`.

When the message handling method returns, the Service Broker Infrastructure calls the `SaveState` method. The `SaveState` method uses the `Connection` property of the Service class to create a `SqlCommand` object, then uses that object to update the state stored in the database. For example, the `ShoppingCartService` deletes the saved contents of the shopping cart and then inserts the current contents of the shopping cart into the database.