microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/close-pull-request

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions/ServiceCollection.cs

130lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using Microsoft.Extensions.DependencyInjection;
5using Microsoft.Extensions.Logging;
6using Microsoft.Teams.Apps.Plugins;
7using Microsoft.Teams.Extensions.Logging;
8
9namespace Microsoft.Teams.Apps.Extensions;
10
11public 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 collection.AddSingleton(provider => factory(provider).GetAwaiter().GetResult());
103 collection.AddSingleton(provider => provider.GetRequiredService<App>().Logger);
104 collection.AddSingleton(provider => provider.GetRequiredService<App>().Storage);
105 collection.AddSingleton<ILogger, TeamsLogger>(provider => new TeamsLogger(provider.GetRequiredService<App>().Logger));
106 collection.AddSingleton<IContext.Accessor>();
107 return collection;
108 }
109
110 public static IServiceCollection AddTeamsPlugin<TPlugin>(this IServiceCollection collection) where TPlugin : class, IPlugin
111 {
112 collection.AddSingleton<TPlugin>();
113 collection.AddSingleton<IPlugin, TPlugin>(provider => provider.GetRequiredService<TPlugin>());
114 return collection.AddHostedService<TeamsPluginService<TPlugin>>();
115 }
116
117 public static IServiceCollection AddTeamsPlugin<TPlugin>(this IServiceCollection collection, TPlugin plugin) where TPlugin : class, IPlugin
118 {
119 collection.AddSingleton(plugin);
120 collection.AddSingleton<IPlugin>(provider => provider.GetRequiredService<TPlugin>());
121 return collection.AddHostedService<TeamsPluginService<TPlugin>>();
122 }
123
124 public static IServiceCollection AddTeamsPlugin<TPlugin>(this IServiceCollection collection, Func<IServiceProvider, TPlugin> factory) where TPlugin : class, IPlugin
125 {
126 collection.AddSingleton(factory);
127 collection.AddSingleton<IPlugin, TPlugin>(factory);
128 return collection.AddHostedService<TeamsPluginService<TPlugin>>();
129 }
130}