以下解决方案使升级后的 Tompanna Agent Handoff 解决方案能够顺利运行:
- 解决方案在于BotFrameworkHttpAdapter需要调用HandoffMiddleware的方式。
检查中间件 https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/47.inspectionGithub 中的示例提供了调用任何中间件的方法,即在我们升级了 Microsoft.Bot.Builder 和引入 BotController 类/API Controller 概念的相关包的情况下。
AdapterWithInspection.cs 的代码参考来自BotBuilder-样本/样本 https://github.com/microsoft/BotBuilder-Samples/blob/main/samples/csharp_dotnetcore/47.inspection/AdapterWithInspection.cs
Replace 检查中间件在下面的代码中切换中间件
namespace Microsoft.BotBuilderSamples
{
public class AdapterWithInspection : BotFrameworkHttpAdapter
{
public AdapterWithInspection(IConfiguration configuration, InspectionState inspectionState, UserState userState, ConversationState conversationState, ILogger<BotFrameworkHttpAdapter> logger)
: base(configuration, logger)
{
// Inspection needs credentiaols because it will be sending the Activities and User and Conversation State to the emulator
var credentials = new MicrosoftAppCredentials(configuration["MicrosoftAppId"], configuration["MicrosoftAppPassword"]);
//***********************************************************************************//
//* InspectionMiddleware needs to be replace HandOffMddieWare in the execution pipeline *//
//***********************************************************************************//
Use(new InspectionMiddleware(inspectionState, userState, conversationState, credentials));
OnTurnError = async (turnContext, exception) =>
{
// Log any leaked exception from the application.
logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");
// Send a message to the user
await turnContext.SendActivityAsync("The bot encountered an error or bug.");
await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");
// Send a trace activity, which will be displayed in the Bot Framework Emulator
await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
};
}
}
}
新代码应如下所示
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
namespace Microsoft.BotBuilderSamples
{
public class AdapterWithInspection : BotFrameworkHttpAdapter
{
public AdapterWithInspection(IConfiguration configuration, InspectionState inspectionState, UserState userState, ConversationState conversationState, ILogger<BotFrameworkHttpAdapter> logger)
: base(configuration, logger)
{
// Inspection needs credentials because it will be sending the Activities and User and Conversation State to the emulator
var credentials = new MicrosoftAppCredentials(configuration["MicrosoftAppId"], configuration["MicrosoftAppPassword"]);
//***********************************************************************************//
//*************** Adding HandOffMddieWare in the execution pipeline *****************//
//***********************************************************************************//
Use(new HandoffMiddleware(configuration));
OnTurnError = async (turnContext, exception) =>
{
// Log any leaked exception from the application.
logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");
// Send a message to the user
await turnContext.SendActivityAsync("The bot encountered an error or bug.");
await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");
// Send a trace activity, which will be displayed in the Bot Framework Emulator
await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
};
}
}
}
NOTE
- 您需要相应地在 Startup.cs 中注入依赖项
在 Startup.cs 中注入依赖项
添加以下代码以方便 AdapterWithInspection 的依赖注入
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithInspection>();