Skip to content

Commit

Permalink
Allow for setting the http client timeout via SharePointPnPHttpTimeou…
Browse files Browse the repository at this point in the history
…t as environment variable or app setting
  • Loading branch information
jansenbe committed Mar 10, 2021
1 parent cb0d9da commit de9dd6e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Hidden webparts can now be skipped during transformation #194 [jansenbe - Bert Jansen]
- Improved Tenant Id fetch method #198 [gautamdsheth - Gautam Sheth]
- Fix if file is template in /libname/Forms folder and therefore has no ListItemId #206 [czullu - Christian Zuellig]
- Allow for setting the http client timeout via SharePointPnPHttpTimeout as environment variable or app setting [jansenbe - Bert Jansen]

## [1.2.0]

Expand Down
40 changes: 39 additions & 1 deletion src/lib/PnP.Framework/Http/PnPHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using System;
using System.Collections.Concurrent;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace PnP.Framework.Http
Expand All @@ -15,13 +17,14 @@ namespace PnP.Framework.Http
/// </summary>
public class PnPHttpClient
{
//private static Configuration configuration;
private const string PnPHttpClientName = "PnPHttpClient";
private static readonly Lazy<PnPHttpClient> _lazyInstance = new Lazy<PnPHttpClient>(() => new PnPHttpClient(), true);
private ServiceProvider serviceProvider;
private static readonly ConcurrentDictionary<string, HttpClientHandler> credentialsHttpClients = new ConcurrentDictionary<string, HttpClientHandler>();

private PnPHttpClient()
{
{
BuildServiceFactory();
}

Expand Down Expand Up @@ -153,10 +156,45 @@ private void BuildServiceFactory()
serviceProvider = serviceCollection.BuildServiceProvider();
}

private static TimeSpan GetHttpTimeout()
{
// get User Agent String
string httpTimeOutValue = null;
try
{
httpTimeOutValue = ConfigurationManager.AppSettings["SharePointPnPHttpTimeout"];
}
catch // throws exception if being called from a .NET Standard 2.0 application
{

}
if (string.IsNullOrWhiteSpace(httpTimeOutValue))
{
httpTimeOutValue = Environment.GetEnvironmentVariable("SharePointPnPHttpTimeout", EnvironmentVariableTarget.Process);
}

if (int.TryParse(httpTimeOutValue, out int httpTimeout))
{
if (httpTimeout == -1)
{
return Timeout.InfiniteTimeSpan;
}
else
{
return new TimeSpan(0, 0, httpTimeout);
}
}

// Return default value of 100 seconds
return new TimeSpan(0, 0, 100);
}

private static IServiceCollection AddHttpClients(IServiceCollection collection, string UserAgent = null)
{
collection.AddHttpClient(PnPHttpClientName, config =>
{
config.Timeout = GetHttpTimeout();

if (string.IsNullOrWhiteSpace(UserAgent))
{
config.DefaultRequestHeaders.UserAgent.TryParseAdd(PnPCoreUtilities.PnPCoreUserAgent);
Expand Down

0 comments on commit de9dd6e

Please sign in to comment.