Skip to content

Commit 4061460

Browse files
author
bjansen
committed
Merge branch 'feature/allow-additional-config-of-httpclients' of https://github.com/kola-tm/pnpcore into pr1659
2 parents 74ef2b4 + 758bdf8 commit 4061460

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

src/sdk/PnP.Core/Services/Builder/PnPCoreServiceCollectionExtensions.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,39 @@ public static class PnPCoreServiceCollectionExtensions
1313
/// <summary>
1414
/// Configures PnP Core SDK with default options
1515
/// </summary>
16+
/// <remarks>
17+
/// <para>
18+
/// Optional additional configuration can be provided for <see cref="SharePointRestClient"/> and/or <see cref="MicrosoftGraphClient"/>
19+
/// </para>
20+
/// </remarks>
1621
/// <param name="services">The collection of services in an <see cref="IServiceCollection" /></param>
22+
/// <param name="configureSharePointRest">additional configuration to <see cref="SharePointRestClient"/></param>
23+
/// <param name="configureMicrosoftGraph">additional configuration to <see cref="MicrosoftGraphClient"/></param>
1724
/// <returns>A PnPCoreBuilder instance</returns>
18-
public static IPnPCoreBuilder AddPnPCore(this IServiceCollection services)
25+
public static IPnPCoreBuilder AddPnPCore(this IServiceCollection services,
26+
Action<IHttpClientBuilder> configureSharePointRest = null,
27+
Action<IHttpClientBuilder> configureMicrosoftGraph = null)
1928
{
20-
return AddPnPCore(services, null);
29+
return AddPnPCore(services, null, configureSharePointRest, configureMicrosoftGraph);
2130
}
2231

2332
/// <summary>
2433
/// Configures PnP Core SDK with custom options
2534
/// </summary>
35+
/// <remarks>
36+
/// <para>
37+
/// Optional additional configuration can be provided for <see cref="SharePointRestClient"/> and/or <see cref="MicrosoftGraphClient"/>
38+
/// </para>
39+
/// </remarks>
2640
/// <param name="services">The collection of services in an <see cref="IServiceCollection" /></param>
2741
/// <param name="options">An Action to configure the PnP Core options</param>
42+
/// <param name="configureSharePointRest">additional configuration to <see cref="SharePointRestClient"/></param>
43+
/// <param name="configureMicrosoftGraph">additional configuration to <see cref="MicrosoftGraphClient"/></param>
2844
/// <returns>A PnPCoreBuilder instance</returns>
2945
public static IPnPCoreBuilder AddPnPCore(this IServiceCollection services,
30-
Action<PnPCoreOptions> options)
46+
Action<PnPCoreOptions> options,
47+
Action<IHttpClientBuilder> configureSharePointRest = null,
48+
Action<IHttpClientBuilder> configureMicrosoftGraph = null)
3149
{
3250
if (services == null)
3351
{
@@ -40,7 +58,7 @@ public static IPnPCoreBuilder AddPnPCore(this IServiceCollection services,
4058
}
4159

4260
services.ConfigureOptions<PnPCoreOptionsConfigurator>();
43-
services.AddPnPContextFactory();
61+
services.AddPnPContextFactory(configureSharePointRest, configureMicrosoftGraph);
4462

4563
return new PnPCoreBuilder(services);
4664
}

src/sdk/PnP.Core/Services/Core/PnPContextFactoryCollectionExtensions.cs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@ public static class PnPContextFactoryCollectionExtensions
1717
/// <summary>
1818
/// Adds the <see cref="PnPContextFactory"/> to the collection of services
1919
/// </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>
2025
/// <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>
2128
/// <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)
2332
{
2433
if (collection == null)
2534
{
@@ -29,17 +38,28 @@ public static IServiceCollection AddPnPContextFactory(this IServiceCollection co
2938
// Add a SharePoint Online Context Factory service instance
3039
return collection
3140
.AddHttpHandlers()
32-
.AddHttpClients()
41+
.AddHttpClients(configureSharePointRest, configureMicrosoftGraph)
3342
.AddPnPServices();
3443
}
3544

3645
/// <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.
3847
/// </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>
3953
/// <param name="collection">Collection of loaded services</param>
4054
/// <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>
4157
/// <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)
4363
{
4464
if (collection == null)
4565
{
@@ -55,7 +75,7 @@ public static IServiceCollection AddPnPContextFactory(this IServiceCollection co
5575
// Add a PnP Context Factory service instance
5676
return collection
5777
.AddHttpHandlers()
58-
.AddHttpClients()
78+
.AddHttpClients(configureSharePointRest, configureMicrosoftGraph)
5979
.AddPnPServices();
6080
}
6181

@@ -69,19 +89,22 @@ private static IServiceCollection AddHttpHandlers(this IServiceCollection collec
6989
return collection;
7090
}
7191

72-
private static IServiceCollection AddHttpClients(this IServiceCollection collection)
92+
private static IServiceCollection AddHttpClients(this IServiceCollection collection, Action<IHttpClientBuilder> configureSharePointRest = null, Action<IHttpClientBuilder> configureMicrosoftGraph = null)
7393
{
94+
IHttpClientBuilder sharePointRestBuilder;
95+
IHttpClientBuilder microsoftGraphBuilder;
96+
7497
#if NET5_0_OR_GREATER
7598
if (RuntimeInformation.RuntimeIdentifier == "browser-wasm")
7699
{
77-
collection.AddHttpClient<SharePointRestClient>()
100+
sharePointRestBuilder = collection.AddHttpClient<SharePointRestClient>()
78101
.AddHttpMessageHandler<SharePointRestRetryHandler>();
79-
collection.AddHttpClient<MicrosoftGraphClient>()
102+
microsoftGraphBuilder = collection.AddHttpClient<MicrosoftGraphClient>()
80103
.AddHttpMessageHandler<MicrosoftGraphRetryHandler>();
81104
}
82105
else
83106
{
84-
collection.AddHttpClient<SharePointRestClient>()
107+
sharePointRestBuilder = collection.AddHttpClient<SharePointRestClient>()
85108
.AddHttpMessageHandler<SharePointRestRetryHandler>()
86109
// We use cookies by adding them to the header which works great when used from Core framework,
87110
// 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
91114
UseCookies = false,
92115
AutomaticDecompression = DecompressionMethods.All
93116
});
94-
collection.AddHttpClient<MicrosoftGraphClient>()
117+
microsoftGraphBuilder = collection.AddHttpClient<MicrosoftGraphClient>()
95118
.AddHttpMessageHandler<MicrosoftGraphRetryHandler>()
96119
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
97120
{
98121
AutomaticDecompression = DecompressionMethods.All
99122
});
100123
}
101124
#else
102-
collection.AddHttpClient<SharePointRestClient>()
125+
sharePointRestBuilder = collection.AddHttpClient<SharePointRestClient>()
103126
.AddHttpMessageHandler<SharePointRestRetryHandler>()
104127
// We use cookies by adding them to the header which works great when used from Core framework,
105128
// 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
109132
UseCookies = false,
110133
AutomaticDecompression = DecompressionMethods.GZip
111134
});
112-
collection.AddHttpClient<MicrosoftGraphClient>()
135+
microsoftGraphBuilder = collection.AddHttpClient<MicrosoftGraphClient>()
113136
.AddHttpMessageHandler<MicrosoftGraphRetryHandler>()
114137
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
115138
{
116139
AutomaticDecompression = DecompressionMethods.GZip
117140
});
118141
#endif
142+
configureSharePointRest?.Invoke(sharePointRestBuilder);
143+
configureMicrosoftGraph?.Invoke(microsoftGraphBuilder);
144+
119145
return collection;
120146
}
121147

0 commit comments

Comments
 (0)