Updated: 5 December 2005
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
- In Visual Studio 2005, open ShoppingCartCS.sln .
- Build the solution by pressing F6, or by selecting Build Solution from the
- Open a command prompt, locate the Scripts folder, and then run the following command: * install all
The install script installs the SQL Server objects for the application, including the common language runtime (CLR) stored procedure that implements the
Running the Sample
- Run the ShoppingCartClient.exe application from the sample directory. For simplicity, the client processes one order each time the client runs.
- Click Create Order 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.
- Click the Service Broker Trace tab at any time to see a summary of the Service Broker messages sent and received.
- The ShoppingList and StateTable tables in the ssb_ShoppingCart database maintain state for the application. The CLR 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
- Open a command prompt, locate the Scripts folder, and then run the following command: * uninstall all *
- The uninstall script removes the SQL Server objects for the application.
This sample requires Visual Studio 2005. Because the sample uses features of the CLR that were not available in earlier versions, versions of Visual Studio earlier than 2005 cannot build the sample.
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.
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 by using the *Service*
class, you implement two methods in your application and a stored procedure in SQL Server, as listed in the following table:
||CREATE PROCEDURE name
||Returns the state for the application. The result sets that are returned by this procedure must contain the conversation group identifier.
||public override bool LoadState(System.Data.Sql.SqlReader reader)
||Reads the result sets that are provided by the stored procedure to restore state. This stored procedure must save the conversation group identifier in the class, because the Service Broker Interface does not provide the identifier to the
||public override void SaveState()
||Saves state to the database.
This topic describes the general outline for restoring state. See
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
property. The Service Broker Interface simply calls the stored procedure provided.
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
as a parameter. The
contains the result sets returned the stored procedure. Typically, implementations of the
method update the
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
stores information about items, quantities, and prices in a private instance of the
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 in addition
to the message type name. In the methods that handle messages, you can read or update the data structures that are created in
When the message handling method returns, the Service Broker Infrastructure calls the
method uses the *Connection*
property of the Service class to create a
object, and then uses that object to update the state stored in the database. For example, the
deletes the saved contents of the shopping cart and then inserts the current contents of the shopping cart into the database.
|5 December 2005
||Changed content: Changed instructions for generating a key file, including the name and location of the key file.
© 2007 Microsoft Corporation. All rights reserved.