Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

LetsGetDefensive - Defensive Coding Practices in C# with Retry Logic and Exception Handling

YouTube Blog Newsletter All Links LinkedIn

This .NET 8.0 solution demonstrates defensive coding practices across three projects: a main console application, a simulated third-party assembly, and an xUnit test suite. The project showcases differentiated exception handling between critical and non-critical operations, retry logic with configurable retry counts for transient failures, proper null handling with nullable reference types, and async/await best practices with ConfigureAwait(false) and CancellationToken support throughout the call chain.

Key patterns include GetAuthTokenAsync() which immediately rethrows UnauthorizedAccessException without retries but retries on other exception types, DoNiceToHaveWorkWithAuth() for graceful failure on non-critical operations, and DoCriticalWorkWithAuth() for operations where exceptions must propagate. The unit test project uses xUnit and Moq with MockRepository.VerifyAll() for strict verification, covering both success and failure paths with parameterized tests using MemberData.

Getting Started

Prerequisites

  • .NET 8.0 SDK or later

Running the Project

cd LetsGetDefensive/LetsGetDefensive
dotnet run

Newsletter

If you found this useful and you want to learn more about C#, .NET, and software engineering, subscribe to the free Dev Leader Weekly newsletter:

Subscribe to Dev Leader Weekly

Connect with Dev Leader


BrandGhost

Powered by BrandGhost 👻