ShowTypingMiddleware doesn't work when configured via AddBot (BotframeworkOptions)
See original GitHub issueVersion
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:
- 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());
});
- 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:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.”
@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 methodsRecommended Bot Setup (MVC)
Instead you want to create the adapter explicitly. And you can add middleware to the pipeline calling
BotFrameworkAdapter.Use()
:ShowTypingMiddleware
Then in
Startup.cs
add that custom adapter as a singleton, which will be given to the Controller via DI:Startup
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 wayAddBot
, 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 useAddBot
in your configurationAnother Older Sample using the
ShowTypingMiddleware
:Enterprise Bot
Startup.cs
FileAll 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