microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
teams.net/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions
Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions/HostApplicationBuilder.cs
105lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Extensions.DependencyInjection; |
| 5 | using Microsoft.Extensions.Hosting; |
| 6 | using Microsoft.Teams.Api.Auth; |
| 7 | using Microsoft.Teams.Apps.Plugins; |
| 8 | using Microsoft.Teams.Common.Logging; |
| 9 | using Microsoft.Teams.Extensions.Logging; |
| 10 | |
| 11 | namespace Microsoft.Teams.Apps.Extensions; |
| 12 | |
| 13 | public static class HostApplicationBuilderExtensions |
| 14 | { |
| 15 | public static IHostApplicationBuilder AddTeamsCore(this IHostApplicationBuilder builder) |
| 16 | { |
| 17 | return AddTeamsCore(builder, new AppOptions()); |
| 18 | } |
| 19 | |
| 20 | public static IHostApplicationBuilder AddTeamsCore(this IHostApplicationBuilder builder, App app) |
| 21 | { |
| 22 | builder.Services.AddSingleton(builder.Configuration.GetTeams()); |
| 23 | builder.Services.AddSingleton(builder.Configuration.GetTeamsLogging()); |
| 24 | builder.Logging.AddTeams(app.Logger); |
| 25 | builder.Services.AddTeams(app); |
| 26 | return builder; |
| 27 | } |
| 28 | |
| 29 | public static IHostApplicationBuilder AddTeamsCore(this IHostApplicationBuilder builder, AppOptions options) |
| 30 | { |
| 31 | var settings = builder.Configuration.GetTeams(); |
| 32 | var loggingSettings = builder.Configuration.GetTeamsLogging(); |
| 33 | |
| 34 | // cloud environment (base preset + per-endpoint overrides) |
| 35 | var cloud = settings.ResolveCloud(options.Cloud); |
| 36 | options.Cloud = cloud; |
| 37 | |
| 38 | // client credentials |
| 39 | if (options.Credentials is null && settings.ClientId is not null && settings.ClientSecret is not null && !settings.Empty) |
| 40 | { |
| 41 | options.Credentials = new ClientCredentials( |
| 42 | settings.ClientId, |
| 43 | settings.ClientSecret, |
| 44 | settings.TenantId |
| 45 | ) |
| 46 | { Cloud = cloud }; |
| 47 | } |
| 48 | else if (options.Credentials is ClientCredentials existingCredentials) |
| 49 | { |
| 50 | existingCredentials.Cloud = cloud; |
| 51 | } |
| 52 | |
| 53 | options.Logger ??= new ConsoleLogger(loggingSettings); |
| 54 | var app = new App(options); |
| 55 | |
| 56 | builder.Services.AddSingleton(settings); |
| 57 | builder.Services.AddSingleton(loggingSettings); |
| 58 | builder.Logging.AddTeams(app.Logger); |
| 59 | builder.Services.AddTeams(app); |
| 60 | return builder; |
| 61 | } |
| 62 | |
| 63 | public static IHostApplicationBuilder AddTeamsCore(this IHostApplicationBuilder builder, AppBuilder appBuilder) |
| 64 | { |
| 65 | var settings = builder.Configuration.GetTeams(); |
| 66 | var loggingSettings = builder.Configuration.GetTeamsLogging(); |
| 67 | |
| 68 | // cloud environment (base preset + per-endpoint overrides) |
| 69 | var cloud = settings.ResolveCloud(); |
| 70 | appBuilder = appBuilder.AddCloud(cloud); |
| 71 | |
| 72 | // client credentials |
| 73 | if (settings.ClientId is not null && settings.ClientSecret is not null && !settings.Empty) |
| 74 | { |
| 75 | var credentials = new ClientCredentials( |
| 76 | settings.ClientId, |
| 77 | settings.ClientSecret, |
| 78 | settings.TenantId |
| 79 | ) |
| 80 | { Cloud = cloud }; |
| 81 | |
| 82 | appBuilder = appBuilder.AddCredentials(credentials); |
| 83 | } |
| 84 | |
| 85 | var app = appBuilder.Build(); |
| 86 | |
| 87 | builder.Services.AddSingleton(settings); |
| 88 | builder.Services.AddSingleton(loggingSettings); |
| 89 | builder.Logging.AddTeams(app.Logger); |
| 90 | builder.Services.AddTeams(app); |
| 91 | return builder; |
| 92 | } |
| 93 | |
| 94 | public static IHostApplicationBuilder AddTeamsPlugin<TPlugin>(this IHostApplicationBuilder builder) where TPlugin : class, IPlugin |
| 95 | { |
| 96 | builder.Services.AddTeamsPlugin<TPlugin>(); |
| 97 | return builder; |
| 98 | } |
| 99 | |
| 100 | public static IHostApplicationBuilder AddTeamsPlugin<TPlugin>(this IHostApplicationBuilder builder, TPlugin plugin) where TPlugin : class, IPlugin |
| 101 | { |
| 102 | builder.Services.AddTeamsPlugin(plugin); |
| 103 | return builder; |
| 104 | } |
| 105 | } |