microsoft / botbuilder-dotnet

Welcome to the Bot Framework SDK for .NET repository, which is the home for the libraries and packages that enable developers to build sophisticated bot applications using .NET.
https://github.com/Microsoft/botframework
MIT License
865 stars 480 forks source link

Getting Unable to resolve service for type Microsoft.Bot.Builder.Dialogs.Dialog #6688

Closed aravinda-sn closed 10 months ago

aravinda-sn commented 10 months ago

Github issues should be used for bugs and feature requests. Use Stack Overflow for general "how-to" questions.

Version

What package version of the SDK are you using? Using Bot Builder framework 4.18.1 Please see screenshot below: image

Describe the bug

Give a clear and concise description of what the bug is: System.AggregateException HResult=0x80131500 Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Bot.Builder.IBot Lifetime: Transient ImplementationType: EchoBot.Bots.DialogBot1[EchoBot.Dialogs.MainDialog]': Unable to resolve service for type 'Microsoft.Bot.Builder.Dialogs.Dialog' while attempting to activate 'EchoBot.Bots.DialogBot1[EchoBot.Dialogs.MainDialog]'.) Source=Microsoft.Extensions.DependencyInjection StackTrace: at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection1 serviceDescriptors, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options) at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder) at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter1.CreateServiceProvider(Object containerBuilder) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at EchoBot.Program.Main(String[] args) in C:\AspNetSamples\ChatBot\EchoBot\EchoBot\Program.cs:line 16

This exception was originally thrown at this call stack: [External Code]

Inner Exception 1: InvalidOperationException: Error while validating the service descriptor 'ServiceType: Microsoft.Bot.Builder.IBot Lifetime: Transient ImplementationType: EchoBot.Bots.DialogBot1[EchoBot.Dialogs.MainDialog]': Unable to resolve service for type 'Microsoft.Bot.Builder.Dialogs.Dialog' while attempting to activate 'EchoBot.Bots.DialogBot1[EchoBot.Dialogs.MainDialog]'.

Inner Exception 2: InvalidOperationException: Unable to resolve service for type 'Microsoft.Bot.Builder.Dialogs.Dialog' while attempting to activate 'EchoBot.Bots.DialogBot`1[EchoBot.Dialogs.MainDialog]'.

To Reproduce

Steps to reproduce the behavior: Run the code in Visual Studio. image using EchoBot.Services; using Microsoft.Bot.Builder.Dialogs; using System; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks;

namespace EchoBot.Dialogs { public class MainDialog : ComponentDialog { private readonly StateService _stateService;

    public MainDialog(StateService stateService) : base(nameof(MainDialog))
    {
        _stateService = stateService ?? throw new ArgumentNullException(nameof(stateService));

        InitializeWaterfallDialog();
    }

    private void InitializeWaterfallDialog()
    {
        var waterfallSteps = new WaterfallStep[]
        {
            InitialStepAsync,
            FinalStepAsync
        };
        //Add named dialogs
        AddDialog(new GreetingDialog($"{nameof(MainDialog)}.greeting", _stateService));
        AddDialog(new BugReportDialog($"{nameof(MainDialog)}.bug", _stateService));

        AddDialog(new WaterfallDialog($"{nameof(MainDialog)}.mainFlow", waterfallSteps));

        //Set the starting dialog
        InitialDialogId = $"{nameof(WaterfallDialog)}.mainFlow";
    }
    private async Task<DialogTurnResult> InitialStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        if (Regex.IsMatch(stepContext.Context.Activity.Text.ToLower(), "hi"))
        {
            return await stepContext.BeginDialogAsync($"{nameof(MainDialog)}.greeting", null, cancellationToken);
        }
        else
        {
            return await stepContext.BeginDialogAsync($"{nameof(MainDialog)}.bug", null, cancellationToken);
        }
    }

    private async Task<DialogTurnResult> FinalStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        return await stepContext.EndDialogAsync(cancellationToken);
    }

}

}

//Below is another cs file using EchoBot.Helpers; using EchoBot.Services; using Microsoft.Bot.Builder; using Microsoft.Bot.Builder.Dialogs; using Microsoft.Bot.Schema; using Microsoft.Extensions.Logging; using System; using System.Threading; using System.Threading.Tasks;

namespace EchoBot.Bots { public class DialogBot : ActivityHandler where T : Dialog {

region Variables

    protected readonly Dialog _dialog;
    protected readonly StateService _stateService;
    protected readonly ILogger _logger;
    #endregion

    public DialogBot(StateService botStateService, Dialog dialog, ILogger<DialogBot<T>> logger)
    {
        _dialog= dialog ?? throw new ArgumentNullException(nameof(dialog));
        _stateService= botStateService ?? throw new ArgumentNullException(nameof(botStateService));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
    {
        await base.OnTurnAsync(turnContext, cancellationToken);
        await _stateService.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
        await _stateService.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
    }

    protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Running Dialog with Message Activity");
        await _dialog.Run(turnContext, _stateService.DialogStateAccessor, cancellationToken);
    }
}

}

Expected behavior

Give a clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem. Attached in message above

Additional context

Add any other context about the problem here. New project with EchoBot template. Added additional code. It is failing to inject

aravinda-sn commented 10 months ago

The error is not related to Bot Framework. This was caused by coding mistake in the constructor parameter list. Please see screenshot. It's working now. So, I am closing the issue. image