问题描述
运行Core Bot C#示例后得到System.AggregateException
.
I got a System.AggregateException
after running my Core Bot C# sample.
在Startup.cs
中,我将类添加如下:
In the Startup.cs
I added the class as followes:
services.AddSingleton<ChitChatRecognizer>();
Recognizer类如下:
The Recognizer Class looks like:
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.AI.QnA;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace CoreBot
{
public class ChitChatRecognizer : ActivityHandler
{
private readonly IConfiguration _configuration;
private readonly ILogger<ChitChatRecognizer> _logger;
private readonly IHttpClientFactory _httpClientFactory;
public ChitChatRecognizer(IConfiguration configuration, ILogger<ChitChatRecognizer> logger, IHttpClientFactory httpClientFactory)
{
_configuration = configuration;
_logger = logger;
_httpClientFactory = httpClientFactory;
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var httpClient = _httpClientFactory.CreateClient();
var qnaMaker = new QnAMaker(new QnAMakerEndpoint
{
KnowledgeBaseId = _configuration["QnAKnowledgebaseId"],
EndpointKey = _configuration["QnAEndpointKey"],
Host = _configuration["QnAEndpointHostName"]
},
null,
httpClient);
_logger.LogInformation("Calling QnA Maker");
var options = new QnAMakerOptions { Top = 1 };
// The actual call to the QnA Maker service.
var response = await qnaMaker.GetAnswersAsync(turnContext, options);
if (response != null && response.Length > 0)
{
await turnContext.SendActivityAsync(MessageFactory.Text(response[0].Answer), cancellationToken);
}
else
{
await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);
}
}
}
}
我什至没有使用Class,但是甚至没有错误也无法启动我的程序.我该怎么办?
I'm not even using the Class yet but can't even start my program without the error.What should I do?
ErrorCode:
ErrorCode:
推荐答案
您的设置很奇怪...您正在制作自己的ChitChatRecognizer
,它是一个机器人(它是从ActivityHandler
派生的),然后再进行DI我以为是对话还是其他漫游器?同样,您似乎也想将QnAMaker(QnA的识别器)视为单例,但是您可以在OnMessageActivityAsync
中对其进行初始化-这意味着除非您的漫游器收到消息活动,否则它不会初始化,因此,如果您尝试初始化还有一些需要QnAMaker
的东西,它在启动时将不存在.
Your setup is odd...you're making your ChitChatRecognizer
, which is a bot (it derives from ActivityHandler
), and then later DI into either a dialog or another bot, I imagine? Also it seems you want to treat QnAMaker (the recognizer for QnA) as a singleton, but you initialize it in OnMessageActivityAsync
--meaning it won't initialize unless your bot receives a message activity, so if you're trying to initialize something else the requires a QnAMaker
, it won't exist on startup.
无论如何,要回答如何将QnAMaker
插入corebot的问题,如果您想将QnAMaker
作为单例添加,则可以在启动时将其作为单例添加,然后在您从bot代码中调用它需要致电QnAMaker.
Anyways, to answer the question of how do you insert QnAMaker
into corebot, if you wanted to add QnAMaker
as a singleton, you can add it as singleton in startup, then call it from your bot code when you need to make a call to QnAMaker.
将QnAMaker添加到Corebot示例
Add QnAMaker to Corebot Sample
在Startup.ConfigureServices
中:
// Register QnAMaker recognizer
services.AddSingleton<MyQnaMaker>();
MyQnAMaker.cs
namespace Microsoft.BotBuilderSamples
{
public class MyQnaMaker
{
public MyQnaMaker(IConfiguration configuration)
{
ChitChatRecognizer = new QnAMaker(new QnAMakerEndpoint
{
KnowledgeBaseId = configuration["QnAKnowledgebaseId"],
EndpointKey = configuration["QnAEndpointKey"],
Host = configuration["QnAEndpointHostName"]
});
}
public QnAMaker ChitChatRecognizer { get; set; }
}
}
DialogAndWelcomeBot.cs构造函数
DI QnAMaker
放入机器人
public DialogAndWelcomeBot(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger, MyQnaMaker myQnAMaker)
: base(conversationState, userState, dialog, logger, myQnAMaker)
DialogBot.cs
public DialogBot(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger, MyQnaMaker qnaMaker)
{
ConversationState = conversationState;
UserState = userState;
Dialog = dialog;
Logger = logger;
MyQnaMaker = qnaMaker;
}
...
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
Logger.LogInformation("Running dialog with Message Activity.");
if (turnContext.Activity.Text == "yo")
{
await CallQnAMaker(turnContext, cancellationToken);
} else {
// Run the Dialog with the new message Activity.
await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>("DialogState"), cancellationToken);
}
}
private async Task CallQnAMaker(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var options = new QnAMakerOptions { Top = 1 };
// The actual call to the QnA Maker service.
var response = await MyQnaMaker.ChitChatRecognizer.GetAnswersAsync(turnContext, options);
if (response != null && response.Length > 0)
{
await turnContext.SendActivityAsync(MessageFactory.Text(response[0].Answer), cancellationToken);
}
else
{
await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);
}
}
根据需要向您的班级添加更多内容,这仅仅是最低要求.在上面的DialogBot.cs
中,您可以看到我有触发QnAMaker
的触发器,前提是用户输入"yo"
作为消息,以进行测试.
Add more to your classes as needed, this is just the bare minimum. Above in DialogBot.cs
, you can see that I have the trigger to call QnAMaker
be if the user types in "yo"
as a message, for testing purposes.
正在运行Corebot
替代
或者,您可以在bot的消息处理程序(例如DialogBot.OnMessageActivityAsync()
)中仅new
新建一个QnAMaker
,类似于示例 11.qnamaker
,然后取消单例.
Alternatively you could just new
up a new QnAMaker
within the bot's message handler (like DialogBot.OnMessageActivityAsync()
), similar to how is done in sample 11.qnamaker
, and do away with the singleton.
这篇关于如何在C#-CoreBot中构造QnA Maker实例类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!