microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/setup-copilot-instructions

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/ApplicationBuilder.cs

73lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using System.Reflection;
5
6using Microsoft.AspNetCore.Builder;
7using Microsoft.Extensions.DependencyInjection;
8using Microsoft.Teams.Apps;
9using Microsoft.Teams.Apps.Annotations;
10using Microsoft.Teams.Apps.Plugins;
11
12namespace Microsoft.Teams.Plugins.AspNetCore.Extensions;
13
14public static partial class ApplicationBuilderExtensions
15{
16 /// <summary>
17 /// initializes/starts your Teams app after
18 /// adding all registered IPlugin's
19 /// </summary>
20 /// <param name="routing">set to false to disable the plugins default http controller endpoints</param>
21 /// <returns>your app instance</returns>
22 public static App UseTeams(this IApplicationBuilder builder, bool routing = true)
23 {
24 var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly();
25 var app = builder.ApplicationServices.GetService<App>() ?? new App(builder.ApplicationServices.GetService<AppOptions>());
26 var plugins = builder.ApplicationServices.GetServices<IPlugin>();
27 var types = assembly.GetTypes();
28
29 foreach (var type in types)
30 {
31 var attribute = type.GetCustomAttribute<TeamsControllerAttribute>();
32
33 if (attribute is null)
34 {
35 continue;
36 }
37
38 var controller = builder.ApplicationServices.GetService(type);
39
40 if (controller is not null)
41 {
42 app.AddController(controller);
43 }
44 }
45
46 foreach (var plugin in plugins)
47 {
48 app.AddPlugin(plugin);
49
50 if (plugin is IAspNetCorePlugin aspNetCorePlugin)
51 {
52 aspNetCorePlugin.Configure(builder);
53 }
54 }
55
56 if (routing)
57 {
58 builder.UseRouting();
59 builder.UseAuthorization();
60 builder.UseEndpoints(endpoints => endpoints.MapControllers());
61 }
62
63 return app;
64 }
65
66 /// <summary>
67 /// get the AspNetCorePlugin instance
68 /// </summary>
69 public static AspNetCorePlugin GetAspNetCorePlugin(this IApplicationBuilder builder)
70 {
71 return builder.ApplicationServices.GetAspNetCorePlugin();
72 }
73}