Skip to content

Stage 2: Create and configure Cosmos DB Account and Container to store ASP.NET Session state

Sanoobk edited this page Sep 25, 2019 · 1 revision

Stage 2: Create and configure Cosmos DB Account and Container to store ASP.NET Session state

Cosmos DB Account and Container was created using the instructions from Microsoft.

Configure appsettings.json key values for AzureSearchSettings.

  • Navigate to Azure Portal Cosmos DB created and in the 'Overview' section, copy the 'URI' and update the appsettings.json 'cosmosServiceEndpoint'.
  • Navigate to 'Containers -> Browse' and copy the corresponding 'Collection ID' and 'Database' and update the appsettings.json 'cosmosDbCollectionName' and 'cosmosDBDatabaseName' respectively.
  • Navigate to 'Settings -> Keys' and copy the corresponding 'Primary Key' and update the appsettings.json 'cosmosDBKey'.

Important - Update Code to extend the Echo Bot template to use Cosmos DB and store Session State.

  • The sample Echo Bot template code used from Step 1 do not use any session data preservation.
  • Please refer the source code of this repository to use the required changes to configure Cosmos DB in code.
  • This step preserves the data provided by the user via DLS on each Turn and saves the input to Cosmos DB.

Key Source Code Excerpts:

Create a Cosmos DB connection using the instantiation below.

_myStorage = new CosmosDbStorage(new CosmosDbStorageOptions
{
AuthKey = CosmosDBKey,
CollectionId = CosmosDBCollectionName,
CosmosDBEndpoint = new Uri(CosmosServiceEndpoint),
DatabaseId = CosmosDBDatabaseName,
});

Below is a sample line of code to write changes to the Cosmos DB using .Net TPL. 'changes' object is a Dictionary that keeps track of the user inputs over each Turn.

// Save the user message to your Storage.
await _myStorage.WriteAsync(changes, cancellationToken);

Quick Test:

Deploy the solution from Visual Studio to the Azure Echo Bot Web App Bot.

  • Perform tests using the 'Web Chat' under Bot Management.
  • Navigate to Cosmos DB Container to validate entry saved as part of the 'Web Chat' conversation.