使用Infobip适配器对接WhatsApp时Bot Framework依赖注入报错
问题:Bot Framework对接Infobip WhatsApp适配器启动报错
问题现象
启动基于Bot Framework开发的C#聊天机器人时,出现依赖注入失败错误,提示无法构造AdapterWithErrorHandler,原因是无法解析BotFrameworkAuthentication服务。
完整错误信息
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'.
提供的代码片段
Startup.cs 配置代码
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>(); }
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); } }
Infobip适配器错误处理类代码
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."); }; }; }
解决方案
核心问题
你同时注册了标准Bot Framework的IBotFrameworkHttpAdapter实现(AdapterWithErrorHandler)和Infobip适配器,但标准适配器依赖BotFrameworkAuthentication服务,而对接Infobip渠道时并不需要这个标准适配器,导致DI容器无法找到对应依赖而报错。
修正步骤
- 移除标准适配器注册:删除Startup.cs中
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();这一行,因为你实际使用的是Infobip适配器,不需要标准Bot Framework HTTP适配器。 - 修正错误处理逻辑:
InfobipHandler中存在嵌套的OnTurnError赋值,会导致逻辑重复,需要移除内层赋值。
修改后的代码
Startup.cs
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>(); }
InfobipHandler
public InfobipHandler(InfobipWhatsAppAdapterOptions infobipWhatsAppOptions, IInfobipWhatsAppClient infobipWhatsAppClient, ILogger<InfobipHandler> logger) : base(infobipWhatsAppOptions, infobipWhatsAppClient, logger) { 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."); }; }
说明
- 对接Infobip渠道时,只需要注册Infobip相关的适配器和服务,标准Bot Framework适配器的依赖(如
BotFrameworkAuthentication)并非必需,移除后即可解决启动报错。 - 修正
OnTurnError的嵌套赋值,确保错误处理逻辑能正常执行。
内容的提问来源于stack exchange,提问作者hamzah jazi
相关产品推荐
相关产品推荐

