Spargine is a collection of open-source assemblies and NuGet packages designed for .NET 10, which I have been developing and maintaining since the release of .NET Framework 2. These assemblies are not only a core part of my projects but are also actively deployed in production environments across several companies I collaborate with.
Get Spargine
You can access the source code and NuGet packages here:
- GitHub: Spargine 10
- NuGet: dotNetDaveNuGet
Introducing ExecutionHelper
Many operations can fail when coding—not due to the code itself but because external resources (e.g., files, networks, services) might be temporarily unavailable. For instance, a web call might fail due to heavy network traffic or server overload.
To manage these intermittent issues, I created ExecutionHelper, which includes two methods:
- ProgressiveRetry()
- ProgressiveRetryAsync()
These methods execute operations with retry logic, allowing for progressive delays between retries. You can specify the number of retries and the delay in milliseconds between each attempt.
Example Usage
Here is a simple example of how to use ProgressiveRetry():
var result = ExecutionHelper.ProgressiveRetry(() =>
{
var drives = DriveHelper.GetRemovableDrives();
}, retryCount: 3, retryWaitMilliseconds: 10);
In this example, the code attempts to get removable drives up to three times, with a 10-millisecond delay between each attempt. The method returns the number of attempts made before the operation succeeded.
Automatic Error Logging
If you want to log any errors automatically, pass an ILogger object into the method like this:
var result = ExecutionHelper.ProgressiveRetry(() =>
{
var drives = DriveHelper.GetRemovableDrives();
}, retryCount: 3, retryWaitMilliseconds: 10, logger: _logger);
Additionally, the result will include a list of any errors encountered during retries, which you can manually log if needed.
Why Use ProgressiveRetry?
When building reliable applications, managing transient failures is crucial. ProgressiveRetry() and ProgressiveRetryAsync() provide an effortless way to add retry logic to your code, helping reduce the risk of failures caused by temporary issues such as network disruptions or resource contention. These methods implement progressive backoff strategies, ensuring your retries happen at increasing intervals, giving resources time to recover.
The next time you need to manage intermittent failures give these methods a try! They can significantly improve your application’s resilience.
Proactive Resource Checks
Before implementing a retry mechanism, it is smart to first check if the required resource is even available. For example, why attempt a web service call if there is no network connection? Checking for connectivity upfront can save processing time and avoid unnecessary retries.
In Spargine, I have created a handy method called IsNetworkAvailable() in the ComputerInfo type. This method helps determine whether the computer has a network connection, allowing you to prevent certain operations when the network is down.
By combining ProgressiveRetry() with proactive resource checks, you can create more robust, efficient applications that minimize failure and improve user experience.
Get Involved!
The success of open-source projects like Spargine relies on community contributions. If you find these updates useful or have ideas for further improvements, I encourage you to contribute by:
- Submitting pull requests
- Reporting issues
- Suggesting new features
Your input is invaluable in making Spargine an even more powerful tool for the .NET community.
If you are interested in contributing or have any questions, feel free to contact me via email at dotnetdave@live.com. Your support and collaboration are greatly appreciated!
Thank you, and happy coding!
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.

