如何将 Azure CosmosDB 设置配置为最新的机器人框架设计?

这是我的初创公司,现在它使用内存存储。如何使用 Cosmos 设置?我在文档中找不到任何示例。除了直接写入存储之外。


 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>>();

    }

在我这样做前:


        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);

        });

     }


交互式爱情
浏览 106回答 1
1回答

互换的青春

它非常相似,您指出的文档实际上展示了一种成功的方法。如果您想使用依赖注入,您可以使用如下内容:public class Startup{&nbsp; &nbsp; public Startup(IConfiguration configuration)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Configuration = configuration;&nbsp; &nbsp; }&nbsp; &nbsp; public IConfiguration Configuration { get; }&nbsp; &nbsp; public void ConfigureServices(IServiceCollection services)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; [...]&nbsp; &nbsp; &nbsp; &nbsp; //services.AddSingleton<IStorage, MemoryStorage>();&nbsp; &nbsp; &nbsp; &nbsp; var cosmosServiceEndpoint = Configuration.GetSection("CosmosServiceEndpoint").Value;&nbsp; &nbsp; &nbsp; &nbsp; var cosmosDBKey = Configuration.GetSection("CosmosDBKey").Value;&nbsp; &nbsp; &nbsp; &nbsp; var cosmosDBDatabaseName = Configuration.GetSection("CosmosDBDatabaseName").Value;&nbsp; &nbsp; &nbsp; &nbsp; var cosmosDBCollectionNameUserState = Configuration.GetSection("CosmosDBCollectionNameUserState").Value;&nbsp; &nbsp; &nbsp; &nbsp; services.AddSingleton<IStorage>(sp => new CosmosDbStorage(new CosmosDbStorageOptions()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AuthKey = cosmosDBKey,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CollectionId = cosmosDBCollectionNameUserState,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CosmosDBEndpoint = new Uri(cosmosServiceEndpoint),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DatabaseId = cosmosDBDatabaseName,&nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; [...]只需确保您的 Cosmos 设置位于appsettings.json
打开App,查看更多内容
随时随地看视频慕课网APP