• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

microsoft / botbuilder-dotnet / 363877

10 Aug 2023 08:52PM UTC coverage: 79.092% (+0.1%) from 78.979%
363877

Pull #6655

CI-PR build

web-flow
Merge 94ad1d11f into fdaed8b69
Pull Request #6655: Implementation of Teams batch APIs

26094 of 32992 relevant lines covered (79.09%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/libraries/integration/Microsoft.Bot.Builder.Integration.AspNet.Core/BotMessageHandler.cs
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the MIT License.
3

4
using System;
5
using System.IO;
6
using System.Text;
7
using System.Threading;
8
using System.Threading.Tasks;
9
using Microsoft.AspNetCore.Http;
10
using Microsoft.Bot.Schema;
11
using Newtonsoft.Json;
12

13
namespace Microsoft.Bot.Builder.Integration.AspNet.Core.Handlers
14
{
15
    /// <summary>
16
    /// A handler to process incoming http requests via using an adapter.
17
    /// </summary>
18
    /// <remarks>
19
    /// BotFrameworkAdapter is still supported but the recommended adapter is `CloudAdapter`.
20
    /// </remarks>
21
    public class BotMessageHandler : BotMessageHandlerBase
22
    {
23
        /// <summary>
24
        /// Deserializes the incoming request using a BotMessageHandler, processes it with an <see cref="IAdapterIntegration"/>
25
        /// and returns an <see cref="InvokeResponse"/>.
26
        /// </summary>
27
        /// <param name="request">A <see cref="HttpRequest"/>.</param>
28
        /// <param name="adapter">An instance of <see cref="IAdapterIntegration"/>.</param>
29
        /// <param name="botCallbackHandler">An instance of <see cref="BotCallbackHandler"/>.</param>
30
        /// <param name="cancellationToken">A <see cref="CancellationToken"/>.</param>
31
        /// <returns>An <see cref="InvokeResponse"/> returned from the adapter.</returns>
32
        protected override async Task<InvokeResponse> ProcessMessageRequestAsync(HttpRequest request, IAdapterIntegration adapter, BotCallbackHandler botCallbackHandler, CancellationToken cancellationToken)
33
        {
34
            Activity activity;
35

36
            using (var memoryStream = new MemoryStream())
×
37
            {
38
                await request.Body.CopyToAsync(memoryStream).ConfigureAwait(false);
×
39

40
                // In the case of request buffering being enabled, we could possibly receive a Stream that needs its position reset,
41
                // but we can't _blindly_ reset in case buffering hasn't been enabled since we'll be working with a non-seekable Stream
42
                // in that case which will throw a NotSupportedException
43
                if (request.Body.CanSeek)
×
44
                {
45
                    request.Body.Position = 0;
×
46
                }
47

48
                memoryStream.Position = 0;
×
49

50
                // Get the request body and deserialize to the Activity object.
51
                // NOTE: We explicitly leave the stream open here so others can still access it (in case buffering was enabled); ASP.NET runtime will always dispose of it anyway
52
                using (var bodyReader = new JsonTextReader(new StreamReader(memoryStream, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 1024, leaveOpen: true)) { MaxDepth = null })
×
53
                {
54
                    activity = BotMessageSerializer.Deserialize<Activity>(bodyReader);
×
55
                }
×
56
            }
×
57

58
            var invokeResponse = await adapter.ProcessActivityAsync(
×
59
                request.Headers["Authorization"],
×
60
                activity,
×
61
                botCallbackHandler,
×
62
                cancellationToken).ConfigureAwait(false);
×
63

64
            return invokeResponse;
×
65
        }
×
66
    }
67
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc