4.4 Bot框架中的AzureBlobStorage实施

时间:2019-05-21 15:48:31

标签: c# botframework

我试图弄清楚如何在新的4.4 Bot框架中使用AzureBlobStorage。所有样本都使用了不适合生产机器人的MemoryStorage。在Startup.cs中初始化AzureBloblStorage的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

内存存储仅用于测试目的,不用于生产用途。持久性存储类型(例如数据库存储)最适合生产机器人。在发布您的机器人之前,请确保将存储设置为Cosmos DB或Blob存储。

为了在Startup.cs中初始化Azure Blob存储,可以在Startup.cs文件中添加以下代码:

// If using Blob Storage. Fill these connection details in from configuration.
var accountName = "<ACCOUNT-NAME>";
var accountKey = "<ACCOUNT-KEY>";
var container = "<your-blob-storage-container-name>";

// Add the implementation through dependency injection (as Scoped because Azure Storage client is not thread safe.)
services.AddScoped<IStore>(_ => new BlobStore(accountName, accountKey, container));

您完整的Startup.cs文件将类似于以下示例文件:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.BotBuilderSamples
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // Create the credential provider to be used with the Bot Framework Adapter.
            services.AddSingleton<ICredentialProvider, ConfigurationCredentialProvider>();

            // Create the Bot Framework Adapter with error handling enabled. 
            services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

            // Create the storage we'll be using for the Dialog state. 
            // If using Blob Storage. Fill these connection details in from configuration.
             var accountName = "<ACCOUNT-NAME>";
             var accountKey = "<ACCOUNT-KEY>";
             var container = "<your-blob-storage-container-name>";
            // Add the implementation through dependency injection (as Scoped because Azure Storage client is not thread safe.)
             services.AddScoped<IStore>(_ => new BlobStore(accountName, accountKey, container));

            // The Dialog that will be run by the bot.
            services.AddSingleton<RootDialog>();

            // Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
            services.AddTransient<IBot, ScaleoutBot<RootDialog>>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseDefaultFiles();
            app.UseStaticFiles();

            //app.UseHttpsRedirection();
            app.UseMvc();
        }
    }
}

希望这会有所帮助。

答案 1 :(得分:0)

它在文档中:请参见此页面https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-storage?view=azure-bot-service-4.0&tabs=csharp#implementation-1

最新的课程是AzureBlobStorage,您可以查看源代码here

简而言之:

  • 使用Nuget软件包Microsoft.Bot.Builder.Azure
  • 在您的Microsoft.Bot.Builder.Azure中添加对Startup.cs的引用
  • 添加以下内容:

代码示例:

// Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
//services.AddSingleton<IStorage, MemoryStorage>();
var blobStorage = new AzureBlobStorage("connectionString", "container");
services.AddSingleton<IStorage>(blobStorage);

你去了!