Simplify dependency management for TeamsBotApplication#521
Conversation
There was a problem hiding this comment.
Pull request overview
Introduces a TeamsBotApplicationDependencies record to bundle constructor dependencies for TeamsBotApplication, updates hosting registration to provide that bundle via DI, and refactors samples/tests to use the new constructor pattern.
Changes:
- Added
TeamsBotApplicationDependenciesrecord and updatedTeamsBotApplicationto be constructed from the bundled dependencies. - Updated
AddTeamsBotApplicationhosting extension to register the bundle and added unit tests validating DI resolution. - Refactored sample bots (ExtAIBot, CustomHosting) and updated unit tests to use the bundled constructor.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| core/test/Microsoft.Teams.Apps.UnitTests/TeamsBotApplicationTests.cs | Updates tests to construct TeamsBotApplication via dependencies bundle. |
| core/test/Microsoft.Teams.Apps.UnitTests/TeamsBotApplicationHostingExtensionsTests.cs | Adds new tests validating DI registration/resolution of the bundle and bundled ctor subclassing. |
| core/test/Microsoft.Teams.Apps.UnitTests/PromptPreviewTests.cs | Updates harness construction to use the dependencies bundle. |
| core/test/Microsoft.Teams.Apps.UnitTests/OAuthFlowTests.cs | Updates harness construction to use the dependencies bundle. |
| core/src/Microsoft.Teams.Apps/TeamsBotApplicationDependencies.cs | Adds the new dependencies bundle record type. |
| core/src/Microsoft.Teams.Apps/TeamsBotApplication.HostingExtensions.cs | Registers TeamsBotApplicationDependencies in DI during AddTeamsBotApplication. |
| core/src/Microsoft.Teams.Apps/TeamsBotApplication.cs | Switches construction to the bundled-dependencies ctor. |
| core/samples/ExtAIBot/Program.cs | Switches sample to register/use a TeamsBotApplication subclass and updates Azure OpenAI config key usage. |
| core/samples/ExtAIBot/ExtAIBotApp.cs | Refactors handler wiring into a TeamsBotApplication subclass using bundled ctor + DI. |
| core/samples/ExtAIBot/appsettings.json | Renames Azure OpenAI config key from ModelId to Deployment. |
| core/samples/CustomHosting/MyTeamsBotApp.cs | Updates sample subclass to use bundled dependencies ctor. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…ms.net into mehak/teamsapp-dep
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
core/src/Microsoft.Teams.Apps/TeamsBotApplication.HostingExtensions.cs:114
TeamsBotApplicationOptionsis intended to be "a single options object" for both Core and Teams settings, butAddTeamsBotApplicationcurrently registers bothTeamsBotApplicationOptionsand a separateBotApplicationOptionsviaAddBotApplication. This means DI consumers resolvingBotApplicationOptionswill not see the Teams options instance (and could diverge ifconfigurechanges inherited properties likeAppIdorProcessActivityTimeout). Consider registeringBotApplicationOptionsto resolve to the same singleton instance asTeamsBotApplicationOptions(override theAddBotApplicationregistration).
// Register TeamsBotApplicationOptions
TeamsBotApplicationOptions teamsOptions = new() { AppId = botConfig.ClientId };
configure?.Invoke(teamsOptions);
services.AddSingleton(teamsOptions);
services.AddBotApplication<TApp>(botConfig);
…ms.net into mehak/teamsapp-dep
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.
Comments suppressed due to low confidence (1)
core/samples/ExtAIBot/ExtAIBotApp.cs:30
- ExtAIBotApp requests
ILogger<ExtAIBotApp>and forwards it toTeamsBotApplication, butTeamsBotApplicationcurrently expectsILogger<TeamsBotApplication>(and forwards toBotApplication). This is a compile-time mismatch; align the logger parameter types (either make the base accept non-genericILoggeror request the expectedILogger<...>here).
This pull request refactors the Teams bot application hosting model to simplify bot setup and handler registration, unify options management, and streamline dependency injection. Key changes include updating the
TeamsBotApplicationconstructor and options pattern, consolidating handler registration into bot subclasses, and improving theApiClientinterface. The sample bots and tests are updated to reflect these changes.TeamsBotApplication and Options Refactoring:
TeamsBotApplicationconstructor now takes anApiClient,IHttpContextAccessor, a logger, and an optionalTeamsBotApplicationOptions, consolidating the previously separate conversation and user token clients into theApiClient. The options class now inherits fromBotApplicationOptionsfor unified configuration.Sample Bot Simplification:
ExtAIBotAppclass constructor, and the DI registration is updated to use the new hosting model.ApiClient Interface Improvements:
ApiClientnow exposesConversationClientandUserTokenClientas internal properties instead of private fields, improving testability and consistency. All constructors and copy methods are updated accordingly.Configuration and Dependency Injection Updates:
AppIdfrom the resolved bot configuration. The ExtAIBot sample'sappsettings.jsonand DI registrations are updated to match the new options and Azure OpenAI configuration pattern.Test and Code Cleanup: