microsoft/teams.net
Publicmirrored fromhttps://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
89lines · 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 | // client credentials |
| 35 | if (options.Credentials is null && settings.ClientId is not null && settings.ClientSecret is not null && !settings.Empty) |
| 36 | { |
| 37 | options.Credentials = new ClientCredentials( |
| 38 | settings.ClientId, |
| 39 | settings.ClientSecret, |
| 40 | settings.TenantId |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | options.Logger ??= new ConsoleLogger(loggingSettings); |
| 45 | var app = new App(options); |
| 46 | |
| 47 | builder.Services.AddSingleton(settings); |
| 48 | builder.Services.AddSingleton(loggingSettings); |
| 49 | builder.Logging.AddTeams(app.Logger); |
| 50 | builder.Services.AddTeams(app); |
| 51 | return builder; |
| 52 | } |
| 53 | |
| 54 | public static IHostApplicationBuilder AddTeamsCore(this IHostApplicationBuilder builder, AppBuilder appBuilder) |
| 55 | { |
| 56 | var settings = builder.Configuration.GetTeams(); |
| 57 | var loggingSettings = builder.Configuration.GetTeamsLogging(); |
| 58 | |
| 59 | // client credentials |
| 60 | if (settings.ClientId is not null && settings.ClientSecret is not null && !settings.Empty) |
| 61 | { |
| 62 | appBuilder = appBuilder.AddCredentials(new ClientCredentials( |
| 63 | settings.ClientId, |
| 64 | settings.ClientSecret, |
| 65 | settings.TenantId |
| 66 | )); |
| 67 | } |
| 68 | |
| 69 | var app = appBuilder.Build(); |
| 70 | |
| 71 | builder.Services.AddSingleton(settings); |
| 72 | builder.Services.AddSingleton(loggingSettings); |
| 73 | builder.Logging.AddTeams(app.Logger); |
| 74 | builder.Services.AddTeams(app); |
| 75 | return builder; |
| 76 | } |
| 77 | |
| 78 | public static IHostApplicationBuilder AddTeamsPlugin<TPlugin>(this IHostApplicationBuilder builder) where TPlugin : class, IPlugin |
| 79 | { |
| 80 | builder.Services.AddTeamsPlugin<TPlugin>(); |
| 81 | return builder; |
| 82 | } |
| 83 | |
| 84 | public static IHostApplicationBuilder AddTeamsPlugin<TPlugin>(this IHostApplicationBuilder builder, TPlugin plugin) where TPlugin : class, IPlugin |
| 85 | { |
| 86 | builder.Services.AddTeamsPlugin(plugin); |
| 87 | return builder; |
| 88 | } |
| 89 | } |