Brain Meme
Brain Meme

Reputation: 61

Bot Emulator Framework Unable to resolve service

I'm following the Azure EchoBot Tutorial for adding states and I was running into an issue running the bot in the Bot Framework Emulator. (The bot can connect. The EchoBot without any modifications will run fine) This is what I added to the ConfigureServices Function in my Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // Create the Bot Framework Adapter.
        services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

        //// Create the User state
        services.AddSingleton<UserState>();

        //// Create the Conversation State
        services.AddSingleton<ConversationState>();

        // Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
        services.AddTransient<IBot, EchoBot>();
    }

This is what I added to EchoBot.cs

private readonly BotState _userState;
    private readonly BotState _conversationState;

    public EchoBot (ConversationState conversationState, UserState userState)
    {
        _conversationState = conversationState;
        _userState = userState;
    }

    protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        var conversationStateAccessors = _conversationState.CreateProperty<ConversationFlow>(nameof(ConversationFlow));
        var flow = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationFlow());
        var userStateAccessors = _userState.CreateProperty<UserProfile>("User");
        var profile = await userStateAccessors.GetAsync(turnContext, () => new UserProfile());

        await _conversationState.SaveChangesAsync(turnContext);
        await _userState.SaveChangesAsync(turnContext);
        await turnContext.SendActivityAsync(MessageFactory.Text($"Echo: {turnContext.Activity.Text}"), cancellationToken);
    }

The error I'm getting in the emulator is just POST 500 directline.conversationUpdate and this is a screenshot of the error I'm getting from the bot console:

There appears to be dependency injection issues for resolving the services I added to Configure Services

Thanks! If there's any clarification needed, I'll do my best to update it.

Upvotes: 2

Views: 302

Answers (1)

Nkosi
Nkosi

Reputation: 247531

ConversationState(IStorage) Constructor requires a derived IStorage but it appears based on the shown ConfigureServices that one was not registered with the IServiceCollection

This will cause an exception when trying to resolve the ConversationState as indicated by the exception message.

An example on line showed the following

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    // Create the Bot Framework Adapter with error handling enabled.
    services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();

    // Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
    services.AddSingleton<IStorage, MemoryStorage>();

    // Create the User state.
    services.AddSingleton<UserState>();

    // Create the Conversation state.
    services.AddSingleton<ConversationState>();

    // Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
    services.AddTransient<IBot, StateManagementBot>();
}

Note the inclusion of a derived IStorage with the addition of a singleton MemoryStorage

// Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
services.AddSingleton<IStorage, MemoryStorage>();

Reference Save user and conversation data

Upvotes: 2

Related Questions