File tree Expand file tree Collapse file tree 5 files changed +55
-3
lines changed Expand file tree Collapse file tree 5 files changed +55
-3
lines changed Original file line number Diff line number Diff line change @@ -350,3 +350,4 @@ MigrationBackup/
350
350
.ionide /
351
351
352
352
353
+ /.idea
Original file line number Diff line number Diff line change @@ -23,7 +23,10 @@ public LWAClient(LWAAuthorizationCredentials lwaAuthorizationCredentials)
23
23
24
24
LWAAuthorizationCredentials = lwaAuthorizationCredentials ;
25
25
LWAAccessTokenRequestMetaBuilder = new LWAAccessTokenRequestMetaBuilder ( ) ;
26
- RestClient = new RestClient ( LWAAuthorizationCredentials . Endpoint . GetLeftPart ( UriPartial . Authority ) ) ;
26
+ // RestClient = new RestClient(LWAAuthorizationCredentials.Endpoint.GetLeftPart(UriPartial.Authority));
27
+ RestClient =
28
+ RestClientSingleton . GetRestClient (
29
+ LWAAuthorizationCredentials . Endpoint . GetLeftPart ( UriPartial . Authority ) ) ;
27
30
}
28
31
29
32
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Net . Http ;
3
+ using RestSharp ;
4
+ using RestSharp . Serializers . NewtonsoftJson ;
5
+
6
+ namespace FikaAmazonAPI . AmazonSpApiSDK . Runtime
7
+ {
8
+ /// <summary>
9
+ /// Get Singleton HttpClient
10
+ /// to fixed: RestClient not properly disposed causes memory / socket leaks
11
+ /// <see cref="https://github.com/abuzuhri/Amazon-SP-API-CSharp/issues/523"/>
12
+ /// </summary>
13
+ public static class RestClientSingleton
14
+ {
15
+ private static readonly HttpClient _httpClient = null ;
16
+
17
+ static RestClientSingleton ( )
18
+ {
19
+ _httpClient = new HttpClient ( new StandardSocketsHttpHandler
20
+ {
21
+ PooledConnectionLifetime = TimeSpan . FromMinutes ( 1 )
22
+ } ) ;
23
+ }
24
+
25
+ /// <summary>
26
+ /// Get RestClient By Singleton HttpClient
27
+ /// </summary>
28
+ /// <param name="baseUrl"></param>
29
+ /// <returns></returns>
30
+ /// <exception cref="ArgumentException"></exception>
31
+ public static RestClient GetRestClient ( string baseUrl )
32
+ {
33
+ if ( ! Uri . TryCreate ( baseUrl , UriKind . Absolute , out var baseUri ) )
34
+ {
35
+ throw new ArgumentException ( $ "argument of { baseUrl } is illegal") ;
36
+ }
37
+
38
+ var restClientOption = new RestClientOptions
39
+ {
40
+ BaseUrl = baseUri ,
41
+ } ;
42
+ return new RestClient ( _httpClient , restClientOption ) . UseNewtonsoftJson ( ) ;
43
+ }
44
+ }
45
+ }
Original file line number Diff line number Diff line change 38
38
<PackageReference Include =" AWSSDK.SecurityToken" Version =" 3.7.100.52" />
39
39
<PackageReference Include =" AWSSDK.SQS" Version =" 3.7.100.52" />
40
40
<PackageReference Include =" Microsoft.CSharp" Version =" 4.7.0" />
41
+ <PackageReference Include =" Microsoft.Extensions.Http" Version =" 7.0.0" />
41
42
<PackageReference Include =" Newtonsoft.Json" Version =" 13.0.2" />
42
43
<PackageReference Include =" RestSharp" Version =" 108.0.3" />
43
44
<PackageReference Include =" RestSharp.Serializers.NewtonsoftJson" Version =" 108.0.3" />
45
+ <PackageReference Include =" StandardSocketsHttpHandler" Version =" 2.2.0.4" />
44
46
<PackageReference Include =" System.Collections" Version =" 4.3.0" />
45
47
<PackageReference Include =" System.ComponentModel.Annotations" Version =" 5.0.0" />
46
48
<PackageReference Include =" System.Reflection" Version =" 4.3.0" />
Original file line number Diff line number Diff line change @@ -56,8 +56,9 @@ public RequestService(AmazonCredential amazonCredential)
56
56
57
57
private void CreateRequest ( string url , RestSharp . Method method )
58
58
{
59
- RequestClient = new RestClient ( ApiBaseUrl ) ;
60
- RequestClient . UseNewtonsoftJson ( ) ;
59
+ RequestClient = RestClientSingleton . GetRestClient ( ApiBaseUrl ) ;
60
+ // RequestClient = new RestClient(ApiBaseUrl);
61
+ // RequestClient.UseNewtonsoftJson();
61
62
Request = new RestRequest ( url , method ) ;
62
63
}
63
64
You can’t perform that action at this time.
0 commit comments