66using Microsoft . Extensions . Options ;
77using NetCoreForce . Client ;
88using NetCoreForce . Client . Models ;
9+ using Polly ;
910using System ;
1011using System . Threading ;
1112
@@ -29,16 +30,30 @@ public static IServiceCollection AddResilientStreamingClient(
2930 {
3031 var options = sp . GetRequiredService < IOptions < SalesforceConfiguration > > ( ) . Value ;
3132
32- TimeSpan . TryParse ( options . TokenExpiration , out var tokenExperation ) ;
33+ if ( ! TimeSpan . TryParse ( options . TokenExpiration , out var tokenExperation ) )
34+ {
35+ tokenExperation = TimeSpan . FromHours ( 1 ) ;
36+ }
3337
3438 return new AsyncExpiringLazy < AccessTokenResponse > ( async data =>
3539 {
3640 if ( data . Result == null
3741 || DateTime . UtcNow > data . ValidUntil . Subtract ( TimeSpan . FromSeconds ( 30 ) ) )
3842 {
39- var authClient = new AuthenticationClient ( ) ;
43+ var policy = Policy
44+ . Handle < Exception > ( )
45+ . WaitAndRetryAsync (
46+ retryCount : options . Retry ,
47+ sleepDurationProvider : ( retryAttempt ) => TimeSpan . FromSeconds ( Math . Pow ( options . BackoffPower , retryAttempt ) ) ) ;
48+
49+ var authClient = await policy . ExecuteAsync ( async ( ) =>
50+ {
51+ var auth = new AuthenticationClient ( ) ;
52+
53+ await auth . TokenRefreshAsync ( options . RefreshToken , options . ClientId ) ;
4054
41- await authClient . TokenRefreshAsync ( options . RefreshToken , options . ClientId ) ;
55+ return auth ;
56+ } ) ;
4257
4358 return new AsyncExpirationValue < AccessTokenResponse >
4459 {
@@ -55,21 +70,33 @@ public static IServiceCollection AddResilientStreamingClient(
5570 {
5671 var options = sp . GetRequiredService < IOptions < SalesforceConfiguration > > ( ) . Value ;
5772
58- TimeSpan . TryParse ( options . TokenExpiration , out var tokenExperation ) ;
73+ if ( ! TimeSpan . TryParse ( options . TokenExpiration , out var tokenExperation ) )
74+ {
75+ tokenExperation = TimeSpan . FromHours ( 1 ) ;
76+ }
5977
6078 return new AsyncExpiringLazy < ForceClient > ( async data =>
6179 {
6280 if ( data . Result == null
6381 || DateTime . UtcNow > data . ValidUntil . Subtract ( TimeSpan . FromSeconds ( 30 ) ) )
6482 {
65- var authClient = new AuthenticationClient ( ) ;
83+ var policy = Policy
84+ . Handle < Exception > ( )
85+ . WaitAndRetryAsync (
86+ retryCount : options . Retry ,
87+ sleepDurationProvider : ( retryAttempt ) => TimeSpan . FromSeconds ( Math . Pow ( options . BackoffPower , retryAttempt ) ) ) ;
88+
89+ var client = await policy . ExecuteAsync ( async ( ) =>
90+ {
91+ var authClient = new AuthenticationClient ( ) ;
6692
67- await authClient . TokenRefreshAsync ( options . RefreshToken , options . ClientId ) ;
93+ await authClient . TokenRefreshAsync ( options . RefreshToken , options . ClientId ) ;
6894
69- var client = new ForceClient (
70- authClient . AccessInfo . InstanceUrl ,
71- authClient . ApiVersion ,
72- authClient . AccessInfo . AccessToken ) ;
95+ return new ForceClient (
96+ authClient . AccessInfo . InstanceUrl ,
97+ authClient . ApiVersion ,
98+ authClient . AccessInfo . AccessToken ) ;
99+ } ) ;
73100
74101 return new AsyncExpirationValue < ForceClient >
75102 {
0 commit comments