AElf.ExceptionHandler
1.0.0
See the version list below for details.
dotnet add package AElf.ExceptionHandler --version 1.0.0
NuGet\Install-Package AElf.ExceptionHandler -Version 1.0.0
<PackageReference Include="AElf.ExceptionHandler" Version="1.0.0" />
paket add AElf.ExceptionHandler --version 1.0.0
#r "nuget: AElf.ExceptionHandler, 1.0.0"
// Install AElf.ExceptionHandler as a Cake Addin #addin nuget:?package=AElf.ExceptionHandler&version=1.0.0 // Install AElf.ExceptionHandler as a Cake Tool #tool nuget:?package=AElf.ExceptionHandler&version=1.0.0
AOP Exception Module
A demo of AOP Exception Handling.
About The Project
This is a C# class named ExceptionHandler which is an aspect designed to handle exceptions in methods through PostSharp's OnMethodBoundaryAspect. It intercepts methods when they throw an exception and allows for custom exception handling logic. The code leverages aspects to capture method execution and provides a strategy to deal with specific types of exceptions.
Key Components
Inheritance from OnMethodBoundaryAspect of PostSharp:
The ExceptionHandler class extends OnMethodBoundaryAspect, which allows intercepting the method execution at predefined points, specifically when exceptions are thrown (OnException method).Attributes and Fields:
- TargetType, MethodName, and Exception define the target method and exception type to handle.
- Asynchronous Methods: Able to catch exceptions from nested asynchronous methods and supports TPL.
Pros and Cons
Pros:
- Separation of Concerns:
- Exception handling logic is decoupled from the business logic, improving code readability and maintainability.
- Reusability:
- The aspect can be reused across multiple methods and classes, making it a scalable solution for consistent exception handling.
- Dynamic Method Invocation:
- The code uses reflection and expression trees to invoke any method dynamically, allowing for flexibility in how exceptions are handled.
- Aspect-Oriented Approach:
- Reduces code duplication related to exception handling by centralizing the logic in one place.
- Quicker Onboarding:
- The learning cost is reduced because exception handling is encapsulated in one place without the need to learn PostSharp and reflection related code.
Cons:
- Performance Overhead:
- Reflection and dynamic invocation introduce some performance overhead, which might be noticeable in high-throughput systems.
- Limited Compile-time Safety:
- Errors related to method names or parameter mismatches will only be detected at runtime, increasing the potential for runtime exceptions.
Getting Started
Setup
Add the following dependency to your project's Module class:
using AElf.ExceptionHandler;
[DependsOn(
typeof(AOPExceptionModule)
)]
public class MyTemplateModule : AbpModule
This will automatically register the AOPException module and setup your project for instrumentation.
To use the Aspect:
- Define a Method Returning
FlowBehavior
: Create a method in your target class that handles exceptions and returns a Task<ExceptionHandlingStrategy>. The strategy will dictate how the flow of the program should behave (e.g., return, rethrow, throw).
public class ExceptionHandlingService
{
public static async Task<FlowBehavior> HandleException(Exception ex, int i)
{
Console.WriteLine($"Handled exception: {ex.Message}");
await Task.Delay(100);
return ExceptionHandlingStrategy.Rethrow;
}
}
- Apply the Aspect to Your Methods: Use the ExceptionHandler aspect on the methods where you want to handle exceptions.
[ExceptionHandler(typeof(ArgumentNullException), TargetType = typeof(ExceptionHandlingService), MethodName = nameof(ExceptionHandlingService.HandleException))]
public void SomeMethod(int i)
{
// Business logic that may throw exceptions
}
Examples
Example with multiple exception handler:
[ExceptionHandler(typeof(InvalidOperationException), TargetType = typeof(ExceptionHandlingService), MethodName = nameof(ExceptionHandlingService.HandleException))]
[ExceptionHandler(typeof(ArgumentNullException), TargetType = typeof(ExceptionHandlingService), MethodName = nameof(ExceptionHandlingService.HandleException))]
public void SomeMethod(int i)
{
// Business logic that may throw exceptions
}
Or you can have multiple Exceptions:
[ExceptionHandler([typeof(ArgumentNullException), typeof(InvalidOperationException)], TargetType = typeof(ExceptionHandlingService), MethodName = nameof(ExceptionHandlingService.HandleException))]
public void SomeMethod(int i)
{
// Business logic that may throw exceptions
}
Contributing
If you encounter a bug or have a feature request, please use the Issue Tracker. The project is also open to contributions, so feel free to fork the project and open pull requests.
License
Distributed under the Apache License. See License for more information. Distributed under the MIT License. See License for more information.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Microsoft.Orleans.Core.Abstractions (>= 7.2.6)
- Volo.Abp.Core (>= 8.0.5)
-
net8.0
- Microsoft.Orleans.Core.Abstractions (>= 7.2.6)
- Volo.Abp.Core (>= 8.0.5)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on AElf.ExceptionHandler:
Package | Downloads |
---|---|
AElf.ExceptionHandler.ABP
An ExceptionHandler in AOP in the ABP Framework. |
|
AElf.ExceptionHandler.Orleans
An ExceptionHandler in AOP in the Orleans Framework. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.8.2 | 1,134 | 10/29/2024 |
1.8.1 | 102 | 10/28/2024 |
1.8.0 | 273 | 10/21/2024 |
1.7.0 | 985 | 10/15/2024 |
1.6.2 | 547 | 10/14/2024 |
1.6.1 | 70 | 10/14/2024 |
1.6.0 | 78 | 10/14/2024 |
1.5.0 | 181 | 10/11/2024 |
1.4.0 | 102 | 10/11/2024 |
1.3.0 | 338 | 10/10/2024 |
1.2.0 | 114 | 10/10/2024 |
1.1.2 | 90 | 10/10/2024 |
1.1.1 | 108 | 10/9/2024 |
1.1.0 | 78 | 10/9/2024 |
1.0.0 | 83 | 10/9/2024 |