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