Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know that below code is to avoid error throw when the microservice is not available and also configure to do retries to reach the service.

I am confused how "_" is recognized as an integer which is used as a parameter for
WaitAndRetryAsync Method.

FYI : this function accepts a delegate
public static AsyncRetryPolicy<TResult> WaitAndRetryAsync<TResult>(this PolicyBuilder<TResult> policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider);



services.AddHttpClient("RydoProductsService", rydoconfig =>
            {
                rydoconfig.BaseAddress = new Uri(Configuration["Services:Products"]);
            }).AddTransientHttpErrorPolicy(p=>p.WaitAndRetryAsync(5,_=> TimeSpan.FromMilliseconds(500))) ;


What I have tried:

Googled........................................................................
Posted
Updated 21-Jun-21 4:08am

It represents the result of the lambda expression on the right of the => operator. See => operator - C# reference | Microsoft Docs[^].
 
Share this answer
 
v2
Comments
vaibhav1800 19-Jun-21 9:06am    
But then as part of delegate definition we are expecting two parameters right ? One is integer and other is timespan. I see the timespan parameter defined. What about the integer parameter. Or is it optional ? If yes then where it is defined as optional
Richard MacCutchan 19-Jun-21 9:22am    
The integer is there in the first parameter position: 5.
vaibhav1800 19-Jun-21 12:56pm    
That 5 is retrycount. Its not the one mentioned in the delegate.
The WaitAndRetryAsync method accepts two parameters:
  • An integer representing the maximum number of times to retry. You have passed 5.
  • A Func<int, TimeSpan> delegate, which takes a single parameter - the attempt number - and returns the length of time to wait before trying again. You have passed the lambda method: _ => TimeSpan.FromMilliseconds(500)

The _ here is a discard, which simply means that you don't care about the parameter passed to the delegate. Your policy always waits 500ms between attempts.

Implement HTTP call retries with exponential backoff with Polly | Microsoft Docs[^]
Discards - unassigned discardable variables | Microsoft Docs[^]
 
Share this answer
 
Comments
vaibhav1800 22-Jun-21 5:36am    
I never knew the concept of Discard. Beautifully Explained. Thanks a Lot Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900