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/ServiceCollection.cs
131lines · 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.Logging; |
| 6 | using Microsoft.Teams.Apps.Plugins; |
| 7 | using Microsoft.Teams.Extensions.Logging; |
| 8 | |
| 9 | namespace Microsoft.Teams.Apps.Extensions; |
| 10 | |
| 11 | public static class ServiceCollectionExtensions |
| 12 | { |
| 13 | public static IServiceCollection AddTeams(this IServiceCollection collection) |
| 14 | { |
| 15 | collection.AddSingleton<Common.Logging.ConsoleLogger>(); |
| 16 | collection.AddSingleton<Common.Logging.ILogger, Common.Logging.ConsoleLogger>(provider => provider.GetRequiredService<Common.Logging.ConsoleLogger>()); |
| 17 | collection.AddSingleton<Common.Storage.LocalStorage<object>>(); |
| 18 | collection.AddSingleton<Common.Storage.IStorage<string, object>>(provider => provider.GetRequiredService<Common.Storage.LocalStorage<object>>()); |
| 19 | |
| 20 | collection.AddSingleton<TeamsLogger>(); |
| 21 | collection.AddSingleton<ILogger, TeamsLogger>(provider => provider.GetRequiredService<TeamsLogger>()); |
| 22 | collection.AddSingleton<ILoggerFactory, LoggerFactory>(provider => |
| 23 | { |
| 24 | var logger = provider.GetRequiredService<TeamsLogger>(); |
| 25 | return new LoggerFactory([new TeamsLoggerProvider(logger)]); |
| 26 | }); |
| 27 | |
| 28 | collection.AddSingleton(provider => |
| 29 | { |
| 30 | var settings = provider.GetRequiredService<TeamsSettings>(); |
| 31 | var logger = provider.GetRequiredService<Common.Logging.ILogger>(); |
| 32 | return App.Builder(settings.Apply()).AddLogger(logger).Build(); |
| 33 | }); |
| 34 | |
| 35 | collection.AddHostedService<TeamsService>(); |
| 36 | collection.AddSingleton<IContext.Accessor>(); |
| 37 | return collection; |
| 38 | } |
| 39 | |
| 40 | public static IServiceCollection AddTeams(this IServiceCollection collection, AppOptions options) |
| 41 | { |
| 42 | var app = new App(options); |
| 43 | var log = new TeamsLogger(app.Logger); |
| 44 | |
| 45 | collection.AddSingleton(app.Logger); |
| 46 | collection.AddSingleton(app.Storage); |
| 47 | collection.AddSingleton<ILoggerFactory>(_ => new LoggerFactory([new TeamsLoggerProvider(log)])); |
| 48 | collection.AddSingleton<ILogger>(log); |
| 49 | collection.AddSingleton(app); |
| 50 | collection.AddHostedService<TeamsService>(); |
| 51 | collection.AddSingleton<IContext.Accessor>(); |
| 52 | return collection; |
| 53 | } |
| 54 | |
| 55 | public static IServiceCollection AddTeams(this IServiceCollection collection, AppBuilder builder) |
| 56 | { |
| 57 | var app = builder.Build(); |
| 58 | var log = new TeamsLogger(app.Logger); |
| 59 | |
| 60 | collection.AddSingleton(app.Logger); |
| 61 | collection.AddSingleton(app.Storage); |
| 62 | collection.AddSingleton<ILoggerFactory, LoggerFactory>(_ => new LoggerFactory([new TeamsLoggerProvider(log)])); |
| 63 | collection.AddSingleton<ILogger>(log); |
| 64 | collection.AddSingleton(app); |
| 65 | collection.AddHostedService<TeamsService>(); |
| 66 | collection.AddSingleton<IContext.Accessor>(); |
| 67 | return collection; |
| 68 | } |
| 69 | |
| 70 | public static IServiceCollection AddTeams(this IServiceCollection collection, App app) |
| 71 | { |
| 72 | var log = new TeamsLogger(app.Logger); |
| 73 | |
| 74 | collection.AddSingleton(app.Logger); |
| 75 | collection.AddSingleton(app.Storage); |
| 76 | collection.AddSingleton<ILoggerFactory, LoggerFactory>(_ => new LoggerFactory([new TeamsLoggerProvider(log)])); |
| 77 | collection.AddSingleton<ILogger>(log); |
| 78 | collection.AddSingleton(app); |
| 79 | collection.AddHostedService<TeamsService>(); |
| 80 | collection.AddSingleton<IContext.Accessor>(); |
| 81 | return collection; |
| 82 | } |
| 83 | |
| 84 | public static IServiceCollection AddTeams(this IServiceCollection collection, Func<IServiceProvider, App> factory) |
| 85 | { |
| 86 | collection.AddSingleton(provider => provider.GetRequiredService<Common.Logging.ILogger>()); |
| 87 | collection.AddSingleton<ILoggerFactory, LoggerFactory>(); |
| 88 | collection.AddHostedService<TeamsService>(); |
| 89 | collection.AddSingleton(factory); |
| 90 | collection.AddSingleton(provider => provider.GetRequiredService<App>().Logger); |
| 91 | collection.AddSingleton(provider => provider.GetRequiredService<App>().Storage); |
| 92 | collection.AddSingleton<ILogger, TeamsLogger>(provider => new TeamsLogger(provider.GetRequiredService<App>().Logger)); |
| 93 | collection.AddSingleton<IContext.Accessor>(); |
| 94 | return collection; |
| 95 | } |
| 96 | |
| 97 | public static IServiceCollection AddTeams(this IServiceCollection collection, Func<IServiceProvider, Task<App>> factory) |
| 98 | { |
| 99 | collection.AddSingleton(provider => provider.GetRequiredService<Common.Logging.ILogger>()); |
| 100 | collection.AddSingleton<ILoggerFactory, LoggerFactory>(); |
| 101 | collection.AddHostedService<TeamsService>(); |
| 102 | // DI factory delegates must be synchronous; this blocks the calling thread during service resolution. |
| 103 | collection.AddSingleton(provider => factory(provider).GetAwaiter().GetResult()); |
| 104 | collection.AddSingleton(provider => provider.GetRequiredService<App>().Logger); |
| 105 | collection.AddSingleton(provider => provider.GetRequiredService<App>().Storage); |
| 106 | collection.AddSingleton<ILogger, TeamsLogger>(provider => new TeamsLogger(provider.GetRequiredService<App>().Logger)); |
| 107 | collection.AddSingleton<IContext.Accessor>(); |
| 108 | return collection; |
| 109 | } |
| 110 | |
| 111 | public static IServiceCollection AddTeamsPlugin<TPlugin>(this IServiceCollection collection) where TPlugin : class, IPlugin |
| 112 | { |
| 113 | collection.AddSingleton<TPlugin>(); |
| 114 | collection.AddSingleton<IPlugin, TPlugin>(provider => provider.GetRequiredService<TPlugin>()); |
| 115 | return collection.AddHostedService<TeamsPluginService<TPlugin>>(); |
| 116 | } |
| 117 | |
| 118 | public static IServiceCollection AddTeamsPlugin<TPlugin>(this IServiceCollection collection, TPlugin plugin) where TPlugin : class, IPlugin |
| 119 | { |
| 120 | collection.AddSingleton(plugin); |
| 121 | collection.AddSingleton<IPlugin>(provider => provider.GetRequiredService<TPlugin>()); |
| 122 | return collection.AddHostedService<TeamsPluginService<TPlugin>>(); |
| 123 | } |
| 124 | |
| 125 | public static IServiceCollection AddTeamsPlugin<TPlugin>(this IServiceCollection collection, Func<IServiceProvider, TPlugin> factory) where TPlugin : class, IPlugin |
| 126 | { |
| 127 | collection.AddSingleton(factory); |
| 128 | collection.AddSingleton<IPlugin, TPlugin>(factory); |
| 129 | return collection.AddHostedService<TeamsPluginService<TPlugin>>(); |
| 130 | } |
| 131 | } |