将.bot文件中的设置迁移到appsettings时如何修复bot配置

时间:2019-06-04 12:12:50

标签: c# azure botframework appsettings

尝试将我的机器人部署到Azure时遇到问题。当我尝试创建Azure资源时,出现以下错误:error: InvalidBotData, message: Version: Bot Version has an invalid value.我挖了一点点,发现我的机器人是4.3版本,而现在您需要4.4来部署…

我发现Mircosoft已经有解决此问题的方法:https://docs.microsoft.com/bs-latn-ba/azure/bot-service/bot-file-basics?view=azure-bot-service-4.0&tabs=csharp我遵循了步骤,还更改了QnAmaker和Luis的调用方式。但是,当我运行该应用程序时,出现以下错误:System.InvalidOperationException: Unable to resolve service for type 'VacancyBot.VacancyBot.Services.BotServices' while attempting to activate 'VacancyBot.VacancyBotBot'. 我意识到该漫游器不再被添加到任何地方,因此我尝试使用services.AddSingleton<VacancyBotBot>()添加该漫游器,但这没有用。将其添加为瞬态也不起作用。

通常添加机器人的部分是这样的:

var secretKey = Configuration.GetSection("botFileSecret")?.Value;
var botFilePath = Configuration.GetSection("botFilePath")?.Value;

var botConfig = BotConfiguration.Load(botFilePath ?? @".\nlp-with-luis.bot", secretKey);
services.AddSingleton(sp => botConfig ?? throw new InvalidOperationException($"The .bot config file could not be loaded. ({botConfig})"));

var connectedServices = new BotServices(botConfig);
services.AddSingleton(sp => connectedServices);

但是,这不再起作用了,因为从某些方面来说,找不到。\ nlp-with-luis.bot。 (我还没有真正删除.bot文件,但是我猜它现在不再使用它了。)。

我想知道是否有人碰巧知道如何添加机器人,或以使其可以再次使用的方式更改BotConfiguration。我真的希望这是可能的!如果有人需要查看更多代码,请这样说,我将尝试提供它(:

我忘了补充说,我曾尝试将"botFilePath": "VacancyBot.bot", "botFileSecret": "",放回appsettings文件中,但是却导致再次在Azure中遇到相同的错误...

1 个答案:

答案 0 :(得分:1)

.bot文件仍然可以使用,但是您似乎正在尝试结合使用.bot文件和appsettings.json。让我们理直气壮吧。

从appsettings.json开始:您不再需要botFilePathbotFileSecret。相反,应像下面那样构造appsettings.json:

{
  "MicrosoftAppId": "",
  "MicrosoftAppPassword": "",
  "LuisAppId": "",
  "LuisAPIKey": "",
  "LuisAPIHostName": ""
}

MicrosoftAppIdMicrosoftAppPassword现在通过ConfigurationCredentialProvider.cs文件拉入,该文件以后将作为单例添加到Startup.cs中。 ConfigurationCredentialProvider应该如下所示:

using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.Configuration;

namespace CoreBot1
{
    public class ConfigurationCredentialProvider : SimpleCredentialProvider
    {
        public ConfigurationCredentialProvider(IConfiguration configuration)
            : base(configuration["MicrosoftAppId"], configuration["MicrosoftAppPassword"])
        {
        }
    }
}

简短,甜美并切入要点。最后,如下构建您的startup.cs,以添加bot和ICredentialProvider:

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;

using CoreBot1.Bots;
using CoreBot1.Dialogs;

namespace CoreBot1
{
    public class Startup
    {
        public 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 User and Conversation state. (Memory is great for testing purposes.)
            services.AddSingleton<IStorage, MemoryStorage>();

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

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

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