如何使用AppSettings将Bot Services迁移到框架4.3

时间:2019-08-16 13:58:22

标签: botframework

Microsoft Bot Framework 4.3更改了配置Bot弃用.bot文件并将应用程序配置数据迁移到AppSettings.json文件的方式,但它也更改了提供Bot Configuration的方式。我已经阅读了所有文档和下载模板,但是,它没有描述在具有LUIS和QnA等多种服务以及多个数据库的情况下如何从旧设计迁移到新设计。

这是我当前的项目代码,用于加载所有服务:

Startup.cs

services.AddSingleton(sp => botConfig ?? throw new InvalidOperationException($"The .bot config file could not be loaded. ({botConfig})"));
        services.AddSingleton(sp => new BotServices(botConfig, Configuration));

        // Configure endpoint based on current environment
        var service = botConfig.Services.Where(s => s.Type == "endpoint" && s.Name == _environment).FirstOrDefault();
        if (!(service is EndpointService endpointService))
        {
            throw new InvalidOperationException($"The .bot file does not contain an endpoint with name '{_environment}'.");
        }

BotServices.cs

public BotServices(BotConfiguration botConfiguration, IConfiguration configuration)
        {
            foreach (var service in botConfiguration.Services)
            {
                switch (service.Type)
                {
                    case ServiceTypes.Luis:
                        {
                            var luis = (LuisService)service;
                            if (luis == null)
                            {
                                throw new InvalidOperationException("The LUIS service is not configured correctly in your '.bot' file.");
                            }

                            var app = new LuisApplication(luis.AppId, configuration[luis.AppId], luis.GetEndpoint());
                            var recognizer = new LuisRecognizer(app);
                            this.LuisServices.Add(luis.Name, recognizer);
                            break;
                        }

                    case ServiceTypes.QnA:
                        {
                            var qna = (QnAMakerService)service;
                            if (qna == null)
                            {
                                throw new InvalidOperationException("The QnA service is not configured correctly in your '.bot' file.");
                            }

                            var qnaEndpoint = new QnAMakerEndpoint()
                            {
                                KnowledgeBaseId = qna.KbId,
                                EndpointKey = configuration[qna.KbId],
                                Host = qna.Hostname,
                            };

                            var qnaMaker = new QnAMaker(qnaEndpoint);
                            QnAServices.Add(qna.Name, qnaMaker);
                            break;
                        }
                }
            }
        }

将其放置在适当的位置后,我可以通过将BotServices传递给我的机器人来使用任何服务,但是我找不到使用Microsoft Bot Framework 4.3的正确方法

在此问题上的任何帮助,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

您几乎以相同的方式进行操作,但是您不是第一个代码块,而是:

  1. Create an IBotServices Interface for BotServices
using Microsoft.Bot.Builder.AI.Luis;
using Microsoft.Bot.Builder.AI.QnA;

namespace Microsoft.BotBuilderSamples
{
    public interface IBotServices
    {
        LuisRecognizer Dispatch { get; }
        QnAMaker SampleQnA { get; }
    }
}
  1. ...以便您可以use Dependency Injection in Startup.cs
services.AddSingleton<IBotServices, BotServices>();

我建议您查看NLP with Dispatch Sample