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

microsoft / botbuilder-dotnet / 389169

08 May 2024 05:35PM UTC coverage: 78.163% (+0.006%) from 78.157%
389169

push

CI-PR build

web-flow
Converting to new CodeQL suppression syntax (#6787)

* Converting to new CodeQL suppression syntax

* Adding the reason

26180 of 33494 relevant lines covered (78.16%)

0.78 hits per line

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

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

4
using System;
5
using Microsoft.Extensions.DependencyInjection;
6
using Microsoft.Extensions.DependencyInjection.Extensions;
7
using Microsoft.Extensions.Logging;
8
using Microsoft.Extensions.Options;
9

10
namespace Microsoft.Bot.Builder.Integration.AspNet.Core
11
{
12
    /// <summary>
13
    /// A set of extension methods for <see cref="IServiceCollection"/> which provide support for hosting bots with .NET Core.
14
    /// </summary>
15
    /// <seealso cref="ApplicationBuilderExtensions"/>
16
    /// <seealso cref="IAdapterIntegration"/>
17
    /// <seealso cref="IBot"/>
18
    [Obsolete("Use `CloudAdapter` with `ConfigurationBotFrameworkAuthentication` instead to configure bot runtime.", false)]
19
    public static class ServiceCollectionExtensions
20
    {
21
        /// <summary>
22
        /// Adds and optionally configures a <typeparamref name="TBot">specified bot type</typeparamref> to the <see cref="IServiceCollection" />.
23
        /// </summary>
24
        /// <typeparam name="TBot">A concrete type of <see cref="IBot"/> that is to be registered and exposed to the Bot Framework.</typeparam>
25
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
26
        /// <param name="configureAction">A optional callback that, if provided, will be invoked to further configure of the bot.</param>
27
        /// <returns>A reference to this instance after the operation has completed.</returns>
28
        /// <remarks>
29
        ///     The <typeparamref name="TBot"/> will be registered as <see cref="ServiceLifetime.Transient">transient</see> and be instantiated on each turn.
30
        /// </remarks>
31
        /// <seealso cref="IBot"/>
32
        public static IServiceCollection AddBot<TBot>(this IServiceCollection services, Action<BotFrameworkOptions> configureAction = null)
33
            where TBot : class, IBot
34
        {
35
            if (services == null)
1✔
36
            {
37
                throw new ArgumentNullException(nameof(services));
1✔
38
            }
39

40
            if (configureAction != null)
1✔
41
            {
42
                services.Configure(configureAction);
1✔
43
            }
44

45
            return services
1✔
46
                .TryAddBotFrameworkAdapterIntegration()
1✔
47
                .AddTransient<IBot, TBot>();
1✔
48
        }
49

50
        /// <summary>
51
        /// Adds and optionally configures a singleton <paramref name="bot">bot</paramref> instance to the <see cref="IServiceCollection"/>.
52
        /// </summary>
53
        /// <typeparam name="TBot">A concrete type of <see cref="IBot"/> that is to be registered and exposed to the Bot Framework.</typeparam>
54
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
55
        /// <param name="bot">The instance of the bot that will be registered as a <see cref="ServiceLifetime.Singleton"/>.</param>
56
        /// <param name="configureAction">A optional callback that, if provided, will be invoked to further configure of the bot.</param>
57
        /// <returns>A reference to this instance after the operation has completed.</returns>
58
        /// <seealso cref="IBot"/>
59
        public static IServiceCollection AddBot<TBot>(this IServiceCollection services, TBot bot, Action<BotFrameworkOptions> configureAction = null)
60
        where TBot : class, IBot
61
        {
62
            if (services == null)
1✔
63
            {
64
                throw new ArgumentNullException(nameof(services));
×
65
            }
66

67
            if (bot == null)
1✔
68
            {
69
                throw new ArgumentNullException(nameof(bot));
1✔
70
            }
71

72
            if (configureAction != null)
1✔
73
            {
74
                services.Configure(configureAction);
1✔
75
            }
76

77
            return services
1✔
78
                .TryAddBotFrameworkAdapterIntegration()
1✔
79
                .AddSingleton<IBot>(bot);
1✔
80
        }
81

82
        /// <summary>
83
        /// Adds and optionally configures a <typeparamref name="TBot">specified bot type</typeparamref> to the <see cref="IServiceCollection" />.
84
        /// </summary>
85
        /// <typeparam name="TBot">A concrete type of <see cref="IBot"/> that is to be registered and exposed to the Bot Framework.</typeparam>
86
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
87
        /// <param name="botFactory">A factory method that will supply an instance of the <typeparamref name="TBot"/> when invoked.</param>
88
        /// <param name="configureAction">A optional callback that, if provided, will be invoked to further configure of the bot.</param>
89
        /// <returns>A reference to this instance after the operation has completed.</returns>
90
        /// <remarks>
91
        ///     The <paramref name="botFactory">factory</paramref> will be registered as <see cref="ServiceLifetime.Transient">transient</see>
92
        ///     and be invoked on each turn.
93
        /// </remarks>
94
        /// <seealso cref="IBot"/>
95
        public static IServiceCollection AddBot<TBot>(this IServiceCollection services, Func<IServiceProvider, TBot> botFactory, Action<BotFrameworkOptions> configureAction = null)
96
            where TBot : class, IBot
97
        {
98
            if (services == null)
1✔
99
            {
100
                throw new ArgumentNullException(nameof(services));
1✔
101
            }
102

103
            if (botFactory == null)
1✔
104
            {
105
                throw new ArgumentNullException(nameof(botFactory));
1✔
106
            }
107

108
            if (configureAction != null)
1✔
109
            {
110
                services.Configure(configureAction);
×
111
            }
112

113
            return services
1✔
114
                .TryAddBotFrameworkAdapterIntegration()
1✔
115
                .AddSingleton<IBot>(botFactory);
1✔
116
        }
117

118
        /// <summary>
119
        /// Adds the <see cref="BotFrameworkAdapter"/> as the <see cref="IAdapterIntegration"/> which will be used by the integration layer
120
        /// for processing bot requests.
121
        /// </summary>
122
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
123
        /// <param name="configureAction">A optional callback that, if provided, will be invoked to further configure the integration.</param>
124
        /// <returns>A reference to this instance after the operation has completed.</returns>
125
        /// <remarks>
126
        ///     The <see cref="BotFrameworkAdapter"/> will be registered as a <see cref="ServiceLifetime.Singleton">singleton</see>.
127
        ///
128
        ///     NOTE: Calling any of the <c>AddBot</c> overloads those will attempt to implicitly register this for you if there is no
129
        ///     explicit <see cref="IAdapterIntegration"/> already registered in the <paramref name="services"/> collection.
130
        /// </remarks>
131
        /// <seealso cref="AddBot{TBot}(IServiceCollection, Action{BotFrameworkOptions})"/>
132
        /// <seealso cref="BotFrameworkAdapter"/>
133
        /// <seealso cref="IAdapterIntegration"/>
134
        public static IServiceCollection AddBotFrameworkAdapterIntegration(this IServiceCollection services, Action<BotFrameworkOptions> configureAction = null)
135
        {
136
            if (services == null)
1✔
137
            {
138
                throw new ArgumentNullException(nameof(services));
1✔
139
            }
140

141
            if (configureAction != null)
1✔
142
            {
143
                services.Configure(configureAction);
1✔
144
            }
145

146
            return services.AddSingleton<IAdapterIntegration>(BotFrameworkAdapterSingletonFactory);
×
147
        }
148

149
        private static IServiceCollection TryAddBotFrameworkAdapterIntegration(this IServiceCollection services)
150
        {
151
            if (services == null)
1✔
152
            {
153
                throw new ArgumentNullException(nameof(services));
×
154
            }
155

156
            services.TryAddSingleton<IAdapterIntegration>(BotFrameworkAdapterSingletonFactory);
1✔
157

158
            return services;
1✔
159
        }
160

161
        private static BotFrameworkAdapter BotFrameworkAdapterSingletonFactory(IServiceProvider serviceProvider)
162
        {
163
            var options = serviceProvider.GetRequiredService<IOptions<BotFrameworkOptions>>().Value;
1✔
164
            var logger = serviceProvider.GetRequiredService<ILogger<IAdapterIntegration>>();
1✔
165

166
            BotFrameworkAdapter botFrameworkAdapter;
167

168
            if (options.AppCredentials != null)
1✔
169
            {
170
                botFrameworkAdapter = new BotFrameworkAdapter(
1✔
171
                   options.AppCredentials,
1✔
172
                   options.AuthenticationConfiguration,
1✔
173
                   options.ChannelProvider,
1✔
174
                   options.ConnectorClientRetryPolicy,
1✔
175
                   options.HttpClient,
1✔
176
                   null,
1✔
177
                   logger)
1✔
178
                {
1✔
179
                    OnTurnError = options.OnTurnError,
1✔
180
                };
1✔
181
            }
182
            else
183
            {
184
                botFrameworkAdapter = new BotFrameworkAdapter(
1✔
185
                options.CredentialProvider,
1✔
186
                options.AuthenticationConfiguration,
1✔
187
                options.ChannelProvider,
1✔
188
                options.ConnectorClientRetryPolicy,
1✔
189
                options.HttpClient,
1✔
190
                null,
1✔
191
                logger)
1✔
192
                {
1✔
193
                    OnTurnError = options.OnTurnError,
1✔
194
                };
1✔
195
            }
196

197
            foreach (var middleware in options.Middleware)
1✔
198
            {
199
                botFrameworkAdapter.Use(middleware);
1✔
200
            }
201

202
            return botFrameworkAdapter;
1✔
203
        }
204
    }
205
}
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