microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/ApplicationBuilder.cs
98lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Reflection; |
| 5 | |
| 6 | using Microsoft.AspNetCore.Builder; |
| 7 | using Microsoft.AspNetCore.Http; |
| 8 | using Microsoft.Extensions.DependencyInjection; |
| 9 | using Microsoft.Teams.Api.Auth; |
| 10 | using Microsoft.Teams.Apps; |
| 11 | using Microsoft.Teams.Apps.Annotations; |
| 12 | using Microsoft.Teams.Apps.Plugins; |
| 13 | |
| 14 | using static Microsoft.Teams.Plugins.AspNetCore.Extensions.HostApplicationBuilderExtensions; |
| 15 | |
| 16 | namespace Microsoft.Teams.Plugins.AspNetCore.Extensions; |
| 17 | |
| 18 | public static partial class ApplicationBuilderExtensions |
| 19 | { |
| 20 | /// <summary> |
| 21 | /// initializes/starts your Teams app after |
| 22 | /// adding all registered IPlugin's |
| 23 | /// </summary> |
| 24 | /// <param name="routing">set to false to disable the plugins default http controller endpoints</param> |
| 25 | /// <returns>your app instance</returns> |
| 26 | public static App UseTeams(this IApplicationBuilder builder, bool routing = true) |
| 27 | { |
| 28 | var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly(); |
| 29 | var app = builder.ApplicationServices.GetService<App>() ?? new App(builder.ApplicationServices.GetService<AppOptions>()); |
| 30 | var plugins = builder.ApplicationServices.GetServices<IPlugin>(); |
| 31 | var types = assembly.GetTypes(); |
| 32 | |
| 33 | foreach (var type in types) |
| 34 | { |
| 35 | #pragma warning disable CS0618 // Type or member is obsolete |
| 36 | var attribute = type.GetCustomAttribute<TeamsControllerAttribute>(); |
| 37 | #pragma warning restore CS0618 // Type or member is obsolete |
| 38 | |
| 39 | if (attribute is null) |
| 40 | { |
| 41 | continue; |
| 42 | } |
| 43 | |
| 44 | var controller = builder.ApplicationServices.GetService(type); |
| 45 | |
| 46 | if (controller is not null) |
| 47 | { |
| 48 | #pragma warning disable CS0618 // Type or member is obsolete |
| 49 | app.AddController(controller); |
| 50 | #pragma warning restore CS0618 // Type or member is obsolete |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | foreach (var plugin in plugins) |
| 55 | { |
| 56 | app.AddPlugin(plugin); |
| 57 | |
| 58 | if (plugin is IAspNetCorePlugin aspNetCorePlugin) |
| 59 | { |
| 60 | aspNetCorePlugin.Configure(builder); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (routing) |
| 65 | { |
| 66 | builder.UseRouting(); |
| 67 | builder.UseAuthorization(); |
| 68 | |
| 69 | // Get AspNetCorePlugin for endpoint registration |
| 70 | var aspNetCorePlugin = plugins.OfType<AspNetCorePlugin>().FirstOrDefault(); |
| 71 | |
| 72 | builder.UseEndpoints(endpoints => |
| 73 | { |
| 74 | // Map AspNetCorePlugin endpoint |
| 75 | if (aspNetCorePlugin is not null) |
| 76 | { |
| 77 | endpoints.MapPost("/api/messages", async (HttpContext httpContext, CancellationToken cancellationToken) => |
| 78 | { |
| 79 | return await aspNetCorePlugin.Do(httpContext, cancellationToken); |
| 80 | }).RequireAuthorization(TeamsTokenAuthConstants.AuthorizationPolicy); |
| 81 | } |
| 82 | |
| 83 | // Map controller endpoints (obsolete) |
| 84 | endpoints.MapControllers(); |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | return app; |
| 89 | } |
| 90 | |
| 91 | /// <summary> |
| 92 | /// get the AspNetCorePlugin instance |
| 93 | /// </summary> |
| 94 | public static AspNetCorePlugin GetAspNetCorePlugin(this IApplicationBuilder builder) |
| 95 | { |
| 96 | return builder.ApplicationServices.GetAspNetCorePlugin(); |
| 97 | } |
| 98 | } |