Home > Blockchain >  Unhandled exception. System.AggregateException: Some services are not able to be constructed
Unhandled exception. System.AggregateException: Some services are not able to be constructed

Time:08-17

I have a chatbot using Bot framework C# language and I'm trying to connect it to WhatsApp channel using Infobip adapter but I'm getting error message says :Unhandled exception. System.AggregateException: Some services are not able to be constructed

this is the ConfigureServices in startup.cs file:

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>();
}

whenever I try to run the bot an error message appears that says:

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'.

this is the Infobip controller class:

[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);
    }
}

this is the Infocpib adapter error handler class:

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.");
            };
        };

    }

CodePudding user response:

Your error message says that it is unable to resolve service BotFrameworkAuthentication. You want to inject a BotFrameworkAuthentication service because it is being used by AdapterWithErrorHandler

CodePudding user response:

Try adding

services.AddSingleton<BotFrameworkAuthentication, ConfigurationBotFrameworkAuthentication>();

before adding your bot framework adapter.

You will need to add

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

as dependencies in your Startup.cs.

  • Related