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