Skip to content

Add per-call CancellationToken to async client methods#395

Merged
rido-min merged 4 commits intomainfrom
copilot/add-cancellationtoken-to-client-methods
Mar 31, 2026
Merged

Add per-call CancellationToken to async client methods#395
rido-min merged 4 commits intomainfrom
copilot/add-cancellationtoken-to-client-methods

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 26, 2026

I attest that I have verified

All async methods in Microsoft.Teams.Api.Clients used a single _cancellationToken baked in at construction time, making per-call cancellation impossible and shared-instance cancellation a blunt instrument. ReactionClient already had the correct pattern; this brings all other clients into alignment.

Changes

  • Added CancellationToken cancellationToken = default to every async method in: ActivityClient, BotSignInClient, BotTokenClient, ConversationClient, MeetingClient, MemberClient, TeamClient, UserTokenClient
  • Each method resolves the effective token using the same fallback pattern already established in ReactionClient:
// Before
public async Task<Resource?> CreateAsync(string conversationId, IActivity activity)
{
    var res = await _http.SendAsync(req, _cancellationToken);
    ...
}

// After
public async Task<Resource?> CreateAsync(
    string conversationId,
    IActivity activity,
    CancellationToken cancellationToken = default)
{
    var token = cancellationToken != default ? cancellationToken : _cancellationToken;
    var res = await _http.SendAsync(req, token);
    ...
}
  • Updated Samples.TargetedMessages and Samples.Reactions to pass the handler's cancellationToken to all context.Api.* client method calls, using the new per-call overloads.

Fully backwards-compatible — existing callers are unaffected since the parameter defaults to default.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Add per-call CancellationToken to async client methods Add per-call CancellationToken to async client methods Mar 26, 2026
Copilot AI requested a review from rido-min March 26, 2026 14:53
@rido-min rido-min linked an issue Mar 26, 2026 that may be closed by this pull request
@rido-min
Copy link
Copy Markdown
Member

…ken matcher to Moq expressions

Co-authored-by: rido-min <14916339+rido-min@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/teams.net/sessions/cf26ee0c-36ea-433a-acb5-c35ad63353a2
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 26, 2026

@copilot can you fix the build errors? https://github.com/microsoft/teams.net/actions/runs/23601007928/job/68730822637?pr=395

Fixed in f32780a. The BotTokenClient.GetAsync method's new optional CancellationToken parameter caused CS0854 — C# expression trees used by Moq don't allow calls with optional arguments. Updated all Setup/Verify calls in AppTests.cs to explicitly pass It.IsAny<CancellationToken>() as the third argument.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • token.botframework.com
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/teams.net/teams.net/Tests/Microsoft.Teams.Apps.Tests/bin/Debug/net10.0/Microsoft.Teams.Apps.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/teams.net/teams.net/Tests/Microsoft.Teams.Apps.Tests/bin/Debug/net10.0/Microsoft.Teams.Apps.Tests.deps.json /home/REDACTED/work/teams.net/teams.net/Tests/Microsoft.Teams.Apps.Tests/bin/Debug/net10.0/testhost.dll --port 42225 --endpoint 127.0.0.1:042225 --role client --parentprocessid 4289 --telemetryoptedin false (dns block)
    • Triggering command: /usr/share/dotnet/dotnet /usr/share/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/teams.net/teams.net/Tests/Microsoft.Teams.Apps.Tests/bin/Debug/net8.0/Microsoft.Teams.Apps.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/teams.net/teams.net/Tests/Microsoft.Teams.Apps.Tests/bin/Debug/net8.0/Microsoft.Teams.Apps.Tests.deps.json /home/REDACTED/work/teams.net/teams.net/Tests/Microsoft.Teams.Apps.Tests/bin/Debug/net8.0/testhost.dll --port 41825 --endpoint 127.0.0.1:041825 --role client --parentprocessid 4282 --telemetryoptedin false (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@rido-min
Copy link
Copy Markdown
Member

@copilot review all the samples to make sure we use the new cancellationToken overload

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 26, 2026

@copilot review all the samples to make sure we use the new cancellationToken overload

Done in a3d2f8a. Two samples were calling context.Api client methods without the new cancellationToken parameter:

  • Samples.TargetedMessages/Program.cs: updated Members.GetAsync, Activities.UpdateTargetedAsync, and Activities.DeleteTargetedAsync to pass cancellationToken
  • Samples.Reactions/Program.cs: updated Reactions.AddAsync (×2) and Reactions.DeleteAsync to pass cancellationToken

The other samples (Samples.Echo, Samples.Cards, Samples.Meetings, etc.) don't call any context.Api.* client methods directly, so no changes were needed there.

@rido-min
Copy link
Copy Markdown
Member

I attest that I have verified

@rido-min rido-min marked this pull request as ready for review March 26, 2026 15:29
Copilot AI review requested due to automatic review settings March 26, 2026 15:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds per-call cancellation support across Microsoft.Teams.Api.Clients async methods (and updates samples/tests accordingly) to avoid relying solely on the instance-level _cancellationToken.

Changes:

  • Added CancellationToken cancellationToken = default to multiple client async methods and used a fallback to _cancellationToken when not provided.
  • Updated samples to pass the handler cancellationToken into client calls.
  • Updated tests/mocks to match the new method signatures.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
Tests/Microsoft.Teams.Apps.Tests/AppTests.cs Updates Moq setups/verifications to include the new CancellationToken parameter.
Samples/Samples.TargetedMessages/Program.cs Passes handler cancellationToken into Conversations/Members and targeted activity APIs.
Samples/Samples.Reactions/Program.cs Passes handler cancellationToken into reaction client calls.
Libraries/Microsoft.Teams.Api/Clients/ActivityClient.cs Adds per-call CancellationToken to activity CRUD + targeted activity methods.
Libraries/Microsoft.Teams.Api/Clients/BotSignInClient.cs Adds per-call CancellationToken to bot sign-in APIs.
Libraries/Microsoft.Teams.Api/Clients/BotTokenClient.cs Adds per-call CancellationToken to bot token acquisition APIs.
Libraries/Microsoft.Teams.Api/Clients/ConversationClient.cs Adds per-call CancellationToken to conversation creation API.
Libraries/Microsoft.Teams.Api/Clients/MeetingClient.cs Adds per-call CancellationToken to meeting APIs.
Libraries/Microsoft.Teams.Api/Clients/MemberClient.cs Adds per-call CancellationToken to member APIs.
Libraries/Microsoft.Teams.Api/Clients/TeamClient.cs Adds per-call CancellationToken to team APIs.
Libraries/Microsoft.Teams.Api/Clients/UserTokenClient.cs Adds per-call CancellationToken to user token APIs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Libraries/Microsoft.Teams.Api/Clients/ActivityClient.cs
Comment thread Libraries/Microsoft.Teams.Api/Clients/BotSignInClient.cs
Comment thread Libraries/Microsoft.Teams.Api/Clients/MeetingClient.cs
Comment thread Libraries/Microsoft.Teams.Api/Clients/UserTokenClient.cs
Comment thread Samples/Samples.TargetedMessages/Program.cs
Comment thread Samples/Samples.TargetedMessages/Program.cs
Comment thread Libraries/Microsoft.Teams.Api/Clients/BotTokenClient.cs
Comment thread Libraries/Microsoft.Teams.Api/Clients/ConversationClient.cs
Comment thread Libraries/Microsoft.Teams.Api/Clients/MemberClient.cs
Comment thread Libraries/Microsoft.Teams.Api/Clients/TeamClient.cs
@rido-min rido-min requested a review from singhk97 March 26, 2026 16:01
@rido-min rido-min merged commit 3d46868 into main Mar 31, 2026
11 of 12 checks passed
@rido-min rido-min deleted the copilot/add-cancellationtoken-to-client-methods branch March 31, 2026 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add per-call CancellationToken to async client methods

5 participants