@@ -17,9 +17,18 @@ public static class PnPContextFactoryCollectionExtensions
17
17
/// <summary>
18
18
/// Adds the <see cref="PnPContextFactory"/> to the collection of services
19
19
/// </summary>
20
+ /// <remarks>
21
+ /// <para>
22
+ /// Optional additional configuration can be provided for <see cref="SharePointRestClient"/> and/or <see cref="MicrosoftGraphClient"/>
23
+ /// </para>
24
+ /// </remarks>
20
25
/// <param name="collection">Collection of loaded services</param>
26
+ /// <param name="configureSharePointRest">additional configuration to <see cref="SharePointRestClient"/></param>
27
+ /// <param name="configureMicrosoftGraph">additional configuration to <see cref="MicrosoftGraphClient"/></param>
21
28
/// <returns>Collection of loaded services</returns>
22
- public static IServiceCollection AddPnPContextFactory ( this IServiceCollection collection )
29
+ public static IServiceCollection AddPnPContextFactory ( this IServiceCollection collection ,
30
+ Action < IHttpClientBuilder > configureSharePointRest = null ,
31
+ Action < IHttpClientBuilder > configureMicrosoftGraph = null )
23
32
{
24
33
if ( collection == null )
25
34
{
@@ -29,17 +38,28 @@ public static IServiceCollection AddPnPContextFactory(this IServiceCollection co
29
38
// Add a SharePoint Online Context Factory service instance
30
39
return collection
31
40
. AddHttpHandlers ( )
32
- . AddHttpClients ( )
41
+ . AddHttpClients ( configureSharePointRest , configureMicrosoftGraph )
33
42
. AddPnPServices ( ) ;
34
43
}
35
44
36
45
/// <summary>
37
- /// Adds the <see cref="PnPContextFactory"/> to the collection of services with options
46
+ /// Adds the <see cref="PnPContextFactory"/> to the collection of services with options.
38
47
/// </summary>
48
+ /// <remarks>
49
+ /// <para>
50
+ /// Optional additional configuration can be provided for <see cref="SharePointRestClient"/> and/or <see cref="MicrosoftGraphClient"/>
51
+ /// </para>
52
+ /// </remarks>
39
53
/// <param name="collection">Collection of loaded services</param>
40
54
/// <param name="options"><see cref="PnPContextFactory"/> configuration options</param>
55
+ /// <param name="configureSharePointRest">additional configuration to <see cref="SharePointRestClient"/></param>
56
+ /// <param name="configureMicrosoftGraph">additional configuration to <see cref="MicrosoftGraphClient"/></param>
41
57
/// <returns>Collection of loaded services</returns>
42
- public static IServiceCollection AddPnPContextFactory ( this IServiceCollection collection , Action < PnPContextFactoryOptions > options )
58
+ public static IServiceCollection AddPnPContextFactory (
59
+ this IServiceCollection collection ,
60
+ Action < PnPContextFactoryOptions > options ,
61
+ Action < IHttpClientBuilder > configureSharePointRest = null ,
62
+ Action < IHttpClientBuilder > configureMicrosoftGraph = null )
43
63
{
44
64
if ( collection == null )
45
65
{
@@ -55,7 +75,7 @@ public static IServiceCollection AddPnPContextFactory(this IServiceCollection co
55
75
// Add a PnP Context Factory service instance
56
76
return collection
57
77
. AddHttpHandlers ( )
58
- . AddHttpClients ( )
78
+ . AddHttpClients ( configureSharePointRest , configureMicrosoftGraph )
59
79
. AddPnPServices ( ) ;
60
80
}
61
81
@@ -69,19 +89,22 @@ private static IServiceCollection AddHttpHandlers(this IServiceCollection collec
69
89
return collection ;
70
90
}
71
91
72
- private static IServiceCollection AddHttpClients ( this IServiceCollection collection )
92
+ private static IServiceCollection AddHttpClients ( this IServiceCollection collection , Action < IHttpClientBuilder > configureSharePointRest = null , Action < IHttpClientBuilder > configureMicrosoftGraph = null )
73
93
{
94
+ IHttpClientBuilder sharePointRestBuilder ;
95
+ IHttpClientBuilder microsoftGraphBuilder ;
96
+
74
97
#if NET5_0_OR_GREATER
75
98
if ( RuntimeInformation . RuntimeIdentifier == "browser-wasm" )
76
99
{
77
- collection . AddHttpClient < SharePointRestClient > ( )
100
+ sharePointRestBuilder = collection . AddHttpClient < SharePointRestClient > ( )
78
101
. AddHttpMessageHandler < SharePointRestRetryHandler > ( ) ;
79
- collection . AddHttpClient < MicrosoftGraphClient > ( )
102
+ microsoftGraphBuilder = collection . AddHttpClient < MicrosoftGraphClient > ( )
80
103
. AddHttpMessageHandler < MicrosoftGraphRetryHandler > ( ) ;
81
104
}
82
105
else
83
106
{
84
- collection . AddHttpClient < SharePointRestClient > ( )
107
+ sharePointRestBuilder = collection . AddHttpClient < SharePointRestClient > ( )
85
108
. AddHttpMessageHandler < SharePointRestRetryHandler > ( )
86
109
// We use cookies by adding them to the header which works great when used from Core framework,
87
110
// however when running the .NET Standard 2.0 version from .NET Framework we explicetely have to
@@ -91,15 +114,15 @@ private static IServiceCollection AddHttpClients(this IServiceCollection collect
91
114
UseCookies = false ,
92
115
AutomaticDecompression = DecompressionMethods . All
93
116
} ) ;
94
- collection . AddHttpClient < MicrosoftGraphClient > ( )
117
+ microsoftGraphBuilder = collection . AddHttpClient < MicrosoftGraphClient > ( )
95
118
. AddHttpMessageHandler < MicrosoftGraphRetryHandler > ( )
96
119
. ConfigurePrimaryHttpMessageHandler ( ( ) => new HttpClientHandler ( )
97
120
{
98
121
AutomaticDecompression = DecompressionMethods . All
99
122
} ) ;
100
123
}
101
124
#else
102
- collection . AddHttpClient < SharePointRestClient > ( )
125
+ sharePointRestBuilder = collection . AddHttpClient < SharePointRestClient > ( )
103
126
. AddHttpMessageHandler < SharePointRestRetryHandler > ( )
104
127
// We use cookies by adding them to the header which works great when used from Core framework,
105
128
// however when running the .NET Standard 2.0 version from .NET Framework we explicetely have to
@@ -109,13 +132,16 @@ private static IServiceCollection AddHttpClients(this IServiceCollection collect
109
132
UseCookies = false ,
110
133
AutomaticDecompression = DecompressionMethods . GZip
111
134
} ) ;
112
- collection . AddHttpClient < MicrosoftGraphClient > ( )
135
+ microsoftGraphBuilder = collection . AddHttpClient < MicrosoftGraphClient > ( )
113
136
. AddHttpMessageHandler < MicrosoftGraphRetryHandler > ( )
114
137
. ConfigurePrimaryHttpMessageHandler ( ( ) => new HttpClientHandler ( )
115
138
{
116
139
AutomaticDecompression = DecompressionMethods . GZip
117
140
} ) ;
118
141
#endif
142
+ configureSharePointRest ? . Invoke ( sharePointRestBuilder ) ;
143
+ configureMicrosoftGraph ? . Invoke ( microsoftGraphBuilder ) ;
144
+
119
145
return collection ;
120
146
}
121
147
0 commit comments