未处理的异常.System.AggregateException:无法构造某些服务



我有一个使用Bot框架C#语言的聊天机器人,我正试图使用Infobip适配器将其连接到WhatsApp频道,但我收到错误消息:未处理的异常。System.AggregateException:某些服务无法构造

这是startup.cs文件中的ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
services.AddSingleton<InfobipWhatsAppAdapterOptions>();
services.AddSingleton<IInfobipWhatsAppClient, InfobipWhatsAppClient>();
services.AddSingleton<InfobipWhatsAppAdapter, InfobipHandler>();
services.AddTransient<IBot, EchoBot>();
}

每当我试图运行机器人时,就会出现一条错误消息,上面写着:

Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Bot.Builder.Integration.AspNet.Core.IBotFrameworkHttpAdapter Lifetime: Singleton ImplementationType: WhatsAppEchoBot.AdapterWithErrorHandler': Unable to resolve service for type 'Microsoft.Bot.Connector.Authentication.BotFrameworkAuthentication' while attempting to activate 'WhatsAppEchoBot.AdapterWithErrorHandler'.)
---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.Bot.Builder.Integration.AspNet.Core.IBotFrameworkHttpAdapter Lifetime: Singleton ImplementationType: WhatsAppEchoBot.AdapterWithErrorHandler': Unable to resolve service for type 'Microsoft.Bot.Connector.Authentication.BotFrameworkAuthentication' while attempting to activate 'WhatsAppEchoBot.AdapterWithErrorHandler'.
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Bot.Connector.Authentication.BotFrameworkAuthentication' while attempting to activate 'WhatsAppEchoBot.AdapterWithErrorHandler'.

这是Infobip控制器类:

[Route("api/infobip/whatsapp")]
[ApiController]
public class InfobipController : ControllerBase
{
private readonly InfobipWhatsAppAdapter Adapter;
private readonly IBot Bot;
public InfobipController(InfobipWhatsAppAdapter adapter, IBot bot)
{
Adapter = adapter;
Bot = bot;
}
[HttpPost]
public async Task PostAsync()
{
// Delegate the processing of the HTTP POST to the adapter.
// The adapter will invoke the bot.
await Adapter.ProcessAsync(Request, Response, Bot);
}
}

这是Infocpib适配器错误处理程序类:

public InfobipHandler(InfobipWhatsAppAdapterOptions infobipWhatsAppOptions, 
IInfobipWhatsAppClient infobipWhatsAppClient, ILogger<InfobipHandler> logger)
: base(infobipWhatsAppOptions, infobipWhatsAppClient, logger)
{
OnTurnError = async (turnContext, exception) =>
{
OnTurnError = async (turnContext, exception) =>
{
// Log any leaked exception from the application.
logger.LogError($"Exception caught : {exception.Message}");
// Send a catch-all apology to the user.
await turnContext.SendActivityAsync("Sorry, it looks like something went 
wrong.");
};
};
}

尝试添加

services.AddSingleton<BotFrameworkAuthentication, ConfigurationBotFrameworkAuthentication>();

在添加bot框架适配器之前。

您需要添加

using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;

作为Startup.cs.中的依赖项

您的错误消息表示无法解析服务BotFrameworkAuthentication。您想要注入BotFrameworkAuthentication服务,因为AdapterWithErrorHandler正在使用它

最新更新