@@ -19,14 +19,14 @@ public static partial class HttpUtils
19
19
private class HttpClientFactory
20
20
{
21
21
private readonly Lazy < HttpMessageHandler > lazyHandler ;
22
- internal HttpClientFactory ( HttpClientHandler handler ) =>
23
- lazyHandler = new Lazy < HttpMessageHandler > ( ( ) => handler , LazyThreadSafetyMode . ExecutionAndPublication ) ;
22
+ internal HttpClientFactory ( Func < HttpClientHandler > handler ) =>
23
+ lazyHandler = new Lazy < HttpMessageHandler > ( ( ) => handler ( ) , LazyThreadSafetyMode . ExecutionAndPublication ) ;
24
24
public HttpClient CreateClient ( ) => new ( lazyHandler . Value , disposeHandler : false ) ;
25
25
}
26
26
27
27
// Ok to use HttpClientHandler which now uses SocketsHttpHandler
28
28
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.cs#L16
29
- public static HttpClientHandler HttpClientHandler { get ; set ; } = new ( ) {
29
+ public static Func < HttpClientHandler > HttpClientHandlerFactory { get ; set ; } = ( ) => new ( ) {
30
30
UseDefaultCredentials = true ,
31
31
AutomaticDecompression = DecompressionMethods . Brotli | DecompressionMethods . Deflate | DecompressionMethods . GZip ,
32
32
} ;
@@ -38,10 +38,21 @@ internal HttpClientFactory(HttpClientHandler handler) =>
38
38
// .Configure<HttpClientFactoryOptions>(options =>
39
39
// options.HttpMessageHandlerBuilderActions.Add(builder => builder.PrimaryHandler = HandlerFactory))
40
40
// .BuildServiceProvider().GetRequiredService<IHttpClientFactory>();
41
-
41
+
42
42
// Escape & BYO IHttpClientFactory
43
- private static HttpClientFactory clientFactory = new ( HttpClientHandler ) ;
44
- public static Func < HttpClient > CreateClient { get ; set ; } = ( ) => clientFactory . CreateClient ( ) ;
43
+ private static HttpClientFactory ? clientFactory ;
44
+ public static Func < HttpClient > CreateClient { get ; set ; } = ( ) => {
45
+ try
46
+ {
47
+ clientFactory ??= new ( HttpClientHandlerFactory ) ;
48
+ return clientFactory . CreateClient ( ) ;
49
+ }
50
+ catch ( Exception ex )
51
+ {
52
+ Tracer . Instance . WriteError ( ex ) ;
53
+ return new HttpClient ( ) ;
54
+ }
55
+ } ;
45
56
46
57
public static HttpClient Create ( ) => CreateClient ( ) ;
47
58
0 commit comments