Search code examples
c#botframeworkazure-cosmosdb

how to configure Azure CosmosDB settings to latest bot framework design?


This is my startup now it uses memory storage. How to use cosmos settings? I can't find any sample on the documentation. Except writing this one writing directy to storage.

  public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IStorage, MemoryStorage>();

        services.AddSingleton<UserState>();

        services.AddSingleton<ConversationState>();

        services.AddSingleton<IBotServices, BotServices>(); 

        services.AddTransient<MainDialog>();

        services.AddTransient<IBot, DialogBot<MainDialog>>();
    }

before i was doing it like this:

        public void ConfigureServices(IServiceCollection services)
    {       
        services.AddBot<BasicBot>(options =>
        {
                var cosmosServiceEndpoint = Configuration.GetSection("CosmosServiceEndpoint").Value;
                var cosmosDBKey = Configuration.GetSection("CosmosDBKey").Value;
                var cosmosDBDatabaseName = Configuration.GetSection("CosmosDBDatabaseName").Value;
                var cosmosDBCollectionNameUserState = Configuration.GetSection("CosmosDBCollectionNameUserState").Value;

                IStorage dataStoreConverstationState = 
                new CosmosDbStorage(new CosmosDbStorageOptions
                {
                    AuthKey = cosmosDBKey,
                    CollectionId = cosmosDBCollectionNameUserState,
                    CosmosDBEndpoint = new Uri(cosmosServiceEndpoint),
                    DatabaseId = cosmosDBDatabaseName,
                });
                var conversationState = new ConversationState(dateStoreConversationState)
                options.State.Add(conversationState);

                IStorage dataStoreUserState =
                new CosmosDbStorage(new CosmosDbStorageOptions
                {
                    AuthKey = cosmosDBKey,
                    CollectionId = cosmosDBCollectionNameUserState,
                    CosmosDBEndpoint = new Uri(cosmosServiceEndpoint),
                    DatabaseId = cosmosDBDatabaseName,
                });
                var userState = new UserState(dataStoreUserState);
                options.State.Add(userState);
        });
     }

Solution

  • It's pretty similar and the docs you pointed to actually show a successful way to do it. If you'd like to use Dependency Injection, you'd use something like:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        public IConfiguration Configuration { get; }
    
        public void ConfigureServices(IServiceCollection services)
        {
            [...]
            //services.AddSingleton<IStorage, MemoryStorage>();
            var cosmosServiceEndpoint = Configuration.GetSection("CosmosServiceEndpoint").Value;
            var cosmosDBKey = Configuration.GetSection("CosmosDBKey").Value;
            var cosmosDBDatabaseName = Configuration.GetSection("CosmosDBDatabaseName").Value;
            var cosmosDBCollectionNameUserState = Configuration.GetSection("CosmosDBCollectionNameUserState").Value;
    
            services.AddSingleton<IStorage>(sp => new CosmosDbStorage(new CosmosDbStorageOptions()
            {
                AuthKey = cosmosDBKey,
                CollectionId = cosmosDBCollectionNameUserState,
                CosmosDBEndpoint = new Uri(cosmosServiceEndpoint),
                DatabaseId = cosmosDBDatabaseName,
            }));
        [...]
    

    Just make sure your Cosmos settings are in appsettings.json