@@ -6,33 +6,60 @@ namespace Masa.Contrib.Service.Caller.HttpClient;
6
6
public static class CallerOptionsExtensions
7
7
{
8
8
public static IHttpClientBuilder UseHttpClient ( this CallerOptions callerOptions ,
9
- Func < MasaHttpClientBuilder > ? clientBuilder = null )
10
- => callerOptions . UseHttpClient ( Microsoft . Extensions . Options . Options . DefaultName , clientBuilder ) ;
9
+ Func < MasaHttpClientBuilder > ? clientBuilder = null ,
10
+ bool alwaysGetNewestHttpClient = false )
11
+ => callerOptions . UseHttpClient ( Microsoft . Extensions . Options . Options . DefaultName , clientBuilder , alwaysGetNewestHttpClient ) ;
11
12
12
13
public static IHttpClientBuilder UseHttpClient ( this CallerOptions callerOptions ,
13
14
string name ,
14
- Func < MasaHttpClientBuilder > ? clientBuilder = null )
15
+ Func < MasaHttpClientBuilder > ? clientBuilder = null ,
16
+ bool alwaysGetNewestHttpClient = false )
15
17
{
16
- var builder = clientBuilder == null ? new MasaHttpClientBuilder ( ) : clientBuilder . Invoke ( ) ;
17
- var httpClientBuilder = callerOptions . Services . AddHttpClient ( name , httpClient
18
- => builder . ConfigureHttpClient ( httpClient ) ) ;
19
-
20
- AddCallerExtensions . AddCaller ( callerOptions , name , serviceProvider
21
- => new HttpClientCaller ( serviceProvider , name , builder . Prefix ) ) ;
22
- return httpClientBuilder ;
18
+ return callerOptions . UseHttpClient ( name , masaHttpClientBuilder =>
19
+ {
20
+ var builder = clientBuilder == null ? new MasaHttpClientBuilder ( ) : clientBuilder . Invoke ( ) ;
21
+ masaHttpClientBuilder . BaseAddress = builder . BaseAddress ;
22
+ masaHttpClientBuilder . Prefix = builder . Prefix ;
23
+ masaHttpClientBuilder . Configure = builder . Configure ;
24
+ } , alwaysGetNewestHttpClient ) ;
23
25
}
24
26
25
27
public static IHttpClientBuilder UseHttpClient ( this CallerOptions callerOptions ,
26
- Action < MasaHttpClientBuilder > ? clientBuilder )
27
- => callerOptions . UseHttpClient ( Microsoft . Extensions . Options . Options . DefaultName , clientBuilder ) ;
28
+ Action < MasaHttpClientBuilder > ? clientBuilder ,
29
+ bool alwaysGetNewestHttpClient = false )
30
+ => callerOptions . UseHttpClient ( Microsoft . Extensions . Options . Options . DefaultName , clientBuilder , alwaysGetNewestHttpClient ) ;
28
31
29
32
public static IHttpClientBuilder UseHttpClient ( this CallerOptions callerOptions ,
30
33
string name ,
31
- Action < MasaHttpClientBuilder > ? clientBuilder )
34
+ Action < MasaHttpClientBuilder > ? clientBuilder ,
35
+ bool alwaysGetNewestHttpClient = false )
32
36
{
33
- MasaHttpClientBuilder builder = new MasaHttpClientBuilder ( ) ;
34
- clientBuilder ? . Invoke ( builder ) ;
35
-
36
- return callerOptions . UseHttpClient ( name , ( ) => builder ) ;
37
+ if ( alwaysGetNewestHttpClient )
38
+ {
39
+ var httpClientBuilder = callerOptions . Services . AddHttpClient ( name ) ;
40
+ AddCallerExtensions . AddCaller ( callerOptions , name , serviceProvider
41
+ =>
42
+ {
43
+ var masaHttpClientBuilder = new MasaHttpClientBuilder ( ) ;
44
+ clientBuilder ? . Invoke ( masaHttpClientBuilder ) ;
45
+ var httpClient = serviceProvider . GetRequiredService < IHttpClientFactory > ( ) . CreateClient ( name ) ;
46
+ masaHttpClientBuilder . ConfigureHttpClient ( httpClient ) ;
47
+ return new HttpClientCaller ( httpClient , serviceProvider , masaHttpClientBuilder . Prefix ) ;
48
+ } ) ;
49
+ return httpClientBuilder ;
50
+ }
51
+ else
52
+ {
53
+ var masaHttpClientBuilder = new MasaHttpClientBuilder ( ) ;
54
+ clientBuilder ? . Invoke ( masaHttpClientBuilder ) ;
55
+ var httpClientBuilder = callerOptions . Services . AddHttpClient ( name , httpClient => masaHttpClientBuilder . ConfigureHttpClient ( httpClient ) ) ;
56
+ AddCallerExtensions . AddCaller ( callerOptions , name , serviceProvider
57
+ =>
58
+ {
59
+ var httpClient = serviceProvider . GetRequiredService < IHttpClientFactory > ( ) . CreateClient ( name ) ;
60
+ return new HttpClientCaller ( httpClient , serviceProvider , masaHttpClientBuilder . Prefix ) ;
61
+ } ) ;
62
+ return httpClientBuilder ;
63
+ }
37
64
}
38
65
}
0 commit comments