-
Notifications
You must be signed in to change notification settings - Fork 263
docs: add DI guide for minimal API callbacks #2740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rajan-chari
wants to merge
2
commits into
main
Choose a base branch
from
docs/di-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...s.md/src/components/include/in-depth-guides/dependency-injection/csharp.incl.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <!-- accessing-services --> | ||
|
|
||
| ### Accessing DI Services in Handlers | ||
|
|
||
| The Teams SDK uses the minimal API callback pattern. Services registered in `builder.Services` are available via `app.Services` after `builder.Build()`. Capture them in your handler closures: | ||
|
|
||
| ```csharp | ||
| var builder = WebApplication.CreateBuilder(args); | ||
| builder.AddTeams(); | ||
|
|
||
| // Register your services | ||
| builder.Services.AddSingleton<IMyService, MyService>(); | ||
|
|
||
| var app = builder.Build(); | ||
| var teams = app.UseTeams(); | ||
|
|
||
| // Resolve from DI and capture in the closure | ||
| var myService = app.Services.GetRequiredService<IMyService>(); | ||
|
|
||
| teams.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| // myService is captured from the outer scope | ||
| var result = await myService.ProcessAsync(context.Activity.Text); | ||
| await context.Send(result, cancellationToken); | ||
| }); | ||
|
|
||
| app.Run(); | ||
| ``` | ||
|
|
||
| This is the same pattern used throughout ASP.NET Core minimal APIs. | ||
|
|
||
| <!-- scoped-services --> | ||
|
|
||
| ### Scoped Services (e.g., DbContext) | ||
|
|
||
| Scoped services are created once per scope. In a controller, ASP.NET Core creates a scope per request automatically. In callbacks, you create the scope yourself: | ||
|
|
||
| ```csharp | ||
| var sp = app.Services; | ||
|
|
||
| teams.OnMessage(async (context, cancellationToken) => | ||
| { | ||
| using var scope = sp.CreateScope(); | ||
| var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>(); | ||
| // dbContext is scoped to this request | ||
| await dbContext.Logs.AddAsync(new LogEntry { Text = context.Activity.Text }); | ||
| await dbContext.SaveChangesAsync(cancellationToken); | ||
| }); | ||
| ``` | ||
|
|
||
| :::warning | ||
| Do not resolve scoped services directly from `app.Services` — this throws `InvalidOperationException` at runtime. Always use `CreateScope()`. | ||
| ::: | ||
|
|
||
| <!-- coming-from-controllers --> | ||
|
|
||
| ### Coming from Controllers? | ||
|
|
||
| If you previously used controller-based bots, you're used to constructor injection: | ||
|
|
||
| ```csharp | ||
| public class MyBot : ActivityHandler | ||
| { | ||
| private readonly IMyService _service; | ||
|
|
||
| public MyBot(IMyService service) | ||
| { | ||
| _service = service; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| In the callback pattern, there's no class and no constructor. You resolve services from `app.Services` and capture them in closures. The result is the same — your handler has access to the service — the mechanism is different. | ||
|
|
||
| <!-- see-also --> | ||
|
|
||
| ### See Also | ||
|
|
||
| - [Samples.Lights](https://github.com/microsoft/teams.net/blob/main/Samples/Samples.Lights/Program.cs#L18) — captures a prompt factory from DI | ||
| - [Middleware guide](/in-depth-guides/observability/middleware) — cross-cutting concerns via `app.Use()` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
teams.md/src/pages/templates/in-depth-guides/dependency-injection.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| --- | ||
| sidebar_position: 6 | ||
| title: 'Dependency Injection' | ||
| summary: Access DI services in minimal API handlers using closure capture and service scoping patterns. | ||
| languages: [csharp] | ||
| --- | ||
|
|
||
| # Dependency Injection | ||
|
|
||
| The Teams SDK uses the minimal API callback pattern. This guide covers how to access | ||
| services registered in your DI container from within handler callbacks. | ||
|
|
||
| ## Accessing Services | ||
|
|
||
| <LanguageInclude section="accessing-services" /> | ||
|
|
||
| ## Scoped Services | ||
|
|
||
| <LanguageInclude section="scoped-services" /> | ||
|
|
||
| ## Coming from Controllers? | ||
|
|
||
| <LanguageInclude section="coming-from-controllers" /> | ||
|
|
||
| ## See Also | ||
|
|
||
| <LanguageInclude section="see-also" /> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will lead to blank pages in other languages I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and rendered error messages. Can use N/A under sections and
missing-pages.jsonfile