question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ShowTypingMiddleware doesn't work when configured via AddBot (BotframeworkOptions)

See original GitHub issue

Version

4.5.1

Describe the bug

If ShowTypingMiddleware is added via AddBot configureAction, it doesn’t work. But if it’s added to the adapter direclty via adapter.Use(new ShowTypingMiddleware()) it works.

To Reproduce

Steps to reproduce the behavior:

  1. Add the middleware via the below code. No typing event is sent.
services.AddBot<T>(options =>
{
    // Password
    options.CredentialProvider = credentialProvider;
    // Add typing middleware
    options.Middleware.Add(new ShowTypingMiddleware());
});
  1. Add the middleware via the below code in the adapter:
this.Use(new ShowTypingMiddleware());

Try emulator and now it works.

Expected behavior

It should work in both cases.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
johnataylorcommented, Aug 15, 2019

The middleware must be added to the Adapter that is being used, perhaps that is not happen. One thing we have seen recently is that old and new ways of initializing things are being mixed and this leads to some confusion.

Previously in our original samples we used this AddBot function to add a single bot in the ASP message handling path. However, following some feedback we modified this to align more directly with the ASP MVC Controller model. In this new world the Adapter is created explicitly and the Controller is given it through the ASP DI mechanism.

If you are using the new approach with an explicit Controller and an Adapter added to the services collection then the AddBot code is irrelevant because it is configuring a different Adapter that is never used behind the scenes.

So take care to not mix the old style “AddBot approach” with the new style “MVC approach.”

2reactions
Zerrythcommented, Dec 5, 2019

@sidecus, to reiterate what John wrote earlier, the recommended way of doing things nowadays is to not to use the old setup way of using AddBot. And with whichever path you choose to head down, then it’s important to not mix old ways of setting up a bot with newer methods


Recommended Bot Setup (MVC)

Instead you want to create the adapter explicitly. And you can add middleware to the pipeline calling BotFrameworkAdapter.Use():

Then in Startup.cs add that custom adapter as a singleton, which will be given to the Controller via DI:

The two linked bullets above are part of our robust Virtual Assistant sample built by our solutions team.

Additional reference to the Adapters in the SDK


The “Old Way” Using AddBot

If you do decide to go the route of using AddBot, be sure to not mix the old and new ways of setting up a bot.

  • Startup.cs using the proper setup for the old way

    • note that if you use AddBot, then you must call .UseBotFramework()
  • Also something to note is that this old way does not use the controller model. You can take a look at the scaffolding the linked DialogPromptBot sample of how you’d need to set your bot up if you want to use AddBot in your configuration

  • Another Older Sample using the ShowTypingMiddleware: Enterprise Bot Startup.cs File


All in all, the recommended method for setting up a bot is to explicitly create the adapter and give it to the Controller via DI as shown above.

If you choose to use the older method of setting up a bot, just take care to implement all the pieces to properly set it up

Read more comments on GitHub >

github_iconTop Results From Across the Web

ShowTypingMiddleware broke my bot when deployed
I'm using the BotFramework V4. It all works locally, but not when I publish it on Azure's WebChat. I've followed the example in...
Read more >
ServiceCollectionExtensions.AddBot Method
Adds and optionally configures a singleton bot instance to the IServiceCollection. AddBot<TBot>(IServiceCollection, Action<BotFrameworkOptions>). Adds and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found