microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.DevTools/DevToolsMiddleware.cs
44lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Bot.Core; |
| 5 | using Microsoft.Teams.Bot.Core.Schema; |
| 6 | |
| 7 | namespace Microsoft.Teams.Bot.DevTools; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Middleware that intercepts incoming activities and errors, emitting events to DevTools UI clients. |
| 11 | /// </summary> |
| 12 | public class DevToolsMiddleware : ITurnMiddleware |
| 13 | { |
| 14 | private readonly DevToolsService _service; |
| 15 | |
| 16 | /// <summary> |
| 17 | /// Creates a new DevToolsMiddleware. |
| 18 | /// </summary> |
| 19 | /// <param name="service">The shared DevTools service.</param> |
| 20 | public DevToolsMiddleware(DevToolsService service) |
| 21 | { |
| 22 | _service = service; |
| 23 | } |
| 24 | |
| 25 | /// <inheritdoc/> |
| 26 | public async Task OnTurnAsync(BotApplication botApplication, CoreActivity activity, NextTurn nextTurn, CancellationToken cancellationToken = default) |
| 27 | { |
| 28 | ArgumentNullException.ThrowIfNull(nextTurn); |
| 29 | |
| 30 | // Emit received event before processing |
| 31 | await _service.EmitReceived(activity, cancellationToken).ConfigureAwait(false); |
| 32 | |
| 33 | try |
| 34 | { |
| 35 | await nextTurn(cancellationToken).ConfigureAwait(false); |
| 36 | } |
| 37 | catch (Exception ex) |
| 38 | { |
| 39 | // Emit error event, then re-throw so BotApplication error handling still works |
| 40 | await _service.EmitError(activity, new { message = ex.Message, stackTrace = ex.StackTrace }, cancellationToken).ConfigureAwait(false); |
| 41 | throw; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |