Warm tip: This article is reproduced from stackoverflow.com, please click
asp.net-core-2.0 botframework

Services.AddTransient() Vs Services.AddBot()

发布于 2020-04-21 10:45:32

In the latest bot samples, we can see that bot is being added to services collection as below

services.AddTransient<IBot, MyBot>();

but in older samples, we saw below approach

services.AddBot<MyBot>(options => { });

Here I am trying to understand the benefits of adding bot using AddTransient() over using AddBot(). What I know is that internally AddBot uses AddTransient only, then why use AddTransient. Referred remarks section from this link.

Questioner
Sandeep Kumar Mahala
Viewed
25
Kyle Delaney 2020-02-06 02:48

You can see in the source code that the AddBot methods are used for automatically adding a bot adapter to DI in addition to the bot and for configuring bot-related options like credentials and error handling. The conventions for using the Bot Builder v4 SDK were very different when those samples were made, and the bot's configuration along with its credentials were loaded from something called a bot file. The current convention for using the SDK is much easier because it takes advantage of ASP.NET Core automatically loading the app's configuration from appsettings.json. Since we're not using AddBot anymore you'll notice that the adapter is added to DI explicitly, and you can configure things like error handling and middleware either by accessing the properties and methods of the adapter directly or by deriving your own adapter class, as seen in the samples.