c# - 将 .bot 文件中的设置迁移到 appsettings 时如何修复机器人配置

标签 c# azure botframework appsettings

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

我发现 Mircosoft 已经针对此处发现的问题提供了解决方案:https://learn.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>() 添加它。但这没有用。将其添加为 transient 也不起作用。

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

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 中再次出现相同的错误...

最佳答案

.bot 文件仍然可以使用,但看起来您正在尝试使用 .bot 文件和 appsettings.json 的组合。让我们帮您理清思路。

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

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

现在通过 ConfigurationCredentialProvider.cs 文件引入 MicrosoftAppIdMicrosoftAppPassword,该文件稍后将作为单例添加到 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,以添加机器人和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();
        }
    }
}

关于c# - 将 .bot 文件中的设置迁移到 appsettings 时如何修复机器人配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56443660/

相关文章:

asp.net - 如何在没有用户名或密码的情况下配置与 Azure Database for PostgreSQL 灵活服务器的连接?

node.js - Azure 函数和 Node.js

c# - 如何取消/退出或退出 Bot Framework 中的 PromptDialog.Choice?

c# - Microsoft Bot Framework 在本地工作,但在远程失败

azure - 如何为我的 Azure VHD 回收未使用的 blob 空间

c# - 从 SysListView32 获取项目文本

c# - 在 C# 中检查一个字符串是否与多个子字符串组合

c# - C# 中的拼字游戏单词查找器

c# - 如何在 LuisActionDialog 传递上下文中从一个意图跳转到另一个意图

c# - 如何为路由实现另一个 var 参数名称