-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceCollectionExtensions.cs
114 lines (96 loc) · 4.92 KB
/
ServiceCollectionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// ReSharper disable ConvertToLocalFunction
namespace Atc.Microsoft.Graph.Client.Extensions;
public static class ServiceCollectionExtensions
{
private static readonly string[] DefaultScopes = { "https://graph.microsoft.com/.default" };
/// <summary>
/// Adds the <see cref="GraphServiceClient"/> to the service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> instance to augment.</param>
/// <param name="graphServiceClient"><see cref="GraphServiceClient"/> to use for the service. If null, one must be available in the service provider when this service is resolved.</param>
/// <returns>The same instance as <paramref name="services"/>.</returns>
public static IServiceCollection AddMicrosoftGraphServices(
this IServiceCollection services,
GraphServiceClient? graphServiceClient = null)
{
Func<IServiceProvider, GraphServiceClient> factory = (serviceProvider)
=> graphServiceClient ?? serviceProvider.GetRequiredService<GraphServiceClient>();
services.AddSingleton(factory);
RegisterGraphServices(services);
return services;
}
/// <summary>
/// Adds the <see cref="GraphServiceClient"/> to the service collection using the provided <see cref="TokenCredential"/> and optional scopes.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> instance to augment.</param>
/// <param name="tokenCredential">The <see cref="TokenCredential"/> to use for authentication.</param>
/// <param name="scopes">Optional array of scopes for the <see cref="GraphServiceClient"/>.</param>
/// <returns>The same instance as <paramref name="services"/>.</returns>
public static IServiceCollection AddMicrosoftGraphServices(
this IServiceCollection services,
TokenCredential tokenCredential,
string[]? scopes = null)
{
services.AddSingleton(_ => new GraphServiceClient(tokenCredential, scopes ?? DefaultScopes));
RegisterGraphServices(services);
return services;
}
/// <summary>
/// Adds the <see cref="GraphServiceClient"/> to the service collection using the provided <see cref="GraphServiceOptions"/> and optional scopes.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> instance to augment.</param>
/// <param name="graphServiceOptions">The <see cref="GraphServiceOptions"/> containing configuration for the service.</param>
/// <param name="scopes">Optional array of scopes for the <see cref="GraphServiceClient"/>.</param>
/// <returns>The same instance as <paramref name="services"/>.</returns>
/// <exception cref="InvalidOperationException">Thrown if the <paramref name="graphServiceOptions"/> are invalid.</exception>
public static IServiceCollection AddMicrosoftGraphServices(
this IServiceCollection services,
GraphServiceOptions graphServiceOptions,
string[]? scopes = null)
{
ArgumentNullException.ThrowIfNull(graphServiceOptions);
if (!graphServiceOptions.IsValid())
{
throw new InvalidOperationException($"Required service '{nameof(GraphServiceOptions)}' is not registered");
}
services.AddSingleton(_ =>
{
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var clientSecretCredential = new ClientSecretCredential(
graphServiceOptions.TenantId,
graphServiceOptions.ClientId,
graphServiceOptions.ClientSecret,
options);
return new GraphServiceClient(clientSecretCredential, scopes ?? DefaultScopes);
});
RegisterGraphServices(services);
return services;
}
private static void RegisterGraphServices(
IServiceCollection services)
{
services.AddGraphService<IOneDriveGraphService, OneDriveGraphService>();
services.AddGraphService<IOutlookGraphService, OutlookGraphService>();
services.AddGraphService<ISharepointGraphService, SharepointGraphService>();
services.AddGraphService<ITeamsGraphService, TeamsGraphService>();
services.AddGraphService<IUsersGraphService, UsersGraphService>();
}
private static void AddGraphService<TService, TImplementation>(
this IServiceCollection services)
where TService : class
where TImplementation : GraphServiceClientWrapper, TService
{
services.AddSingleton<TService, TImplementation>(s =>
{
var loggerFactory = s.GetService<ILoggerFactory>() ?? new NullLoggerFactory();
var graphServiceClient = s.GetRequiredService<GraphServiceClient>();
return (TImplementation)Activator.CreateInstance(
typeof(TImplementation),
loggerFactory,
graphServiceClient)!;
});
}
}