PostSharp Aspects are really powerful in applying some useful Aspects like Handling Exception. If you don’t know about PostShap Exception handling Check this Post.
One of PostSharp powerful Utilities is Multicast which is simply applying an Aspect to a whole Namespace with a single line of code .. Magic isn’t it 🙂
You can Multicast an Aspect by simply adding the following line to the Solution AssemblyInfo.cs File under Properties.
[assembly: ExceptionAspect(AttributeTargetTypes = "AOP_Project.Code.*")]
ExceptionAspect is the name of the Aspect i want to insert and “AOP_Project.Code.*” is the target namespace where all the classes under it will have that Aspect while building the solution.
That’s it now you can apply Exception Aspect to all your solution with a single line 🙂
When ever we are writing any piece of code we should often handle any exception that could occurs because of wrong data or even a bug in our code. What we usually did was using try and catch and catch any exception that could occurs. Imagine in a large solution how many try and catch you will have to write and even imagine if you need to change the way you are handling these exception how many lines of code you will need to replace.
Thanks to PostSharp now we can simply handle all the exceptions in our system through a single function. By using Aspect Oriented Programming we can simply apply the exception handling to all our code.
I will guide you step by step how we can use PostSharp and AOP to handle all our exception. You will find the source code on the following link. Remember to install the PostSharp before running the solution as the Exception Handler wont work if PostSharp is not installed.
- Creating a Console Application Project
In Visual Studio menu select File>New>Project and select the “Console Application” template.
- Installing PostSharp on our Project
In Visual Studio menu select Tools>NuGet Package Manager>Manage NuGet Packages for Solution..
After the Manage NuGet Packages Open Select Online on the Left Then Search for PostSharp and click Install.
It will ask you for the Project you want to install PostSharp in, make sure your project is selected.
Wait until it finish downloading and Installing PostSharp. It should not take long.
If you did everything right you will find a reference in your project to PostSharp
- Creating our Exception Aspect Class
Right Click the Project>Add>Class and name it whatever you want.
Now we need to implement OnExceptionAspect which has a function OnException that we need to override to however you want to handle Exceptions.
public class ExceptionAspect : OnExceptionAspect
public override void OnException(MethodExecutionArgs args)
Console.WriteLine(&amp;amp;quot;Exception : &amp;amp;quot; + args.Exception.Message + &amp;amp;quot; at &amp;amp;quot; + DateTime.Now);
args.FlowBehavior = FlowBehavior.Continue;
as you can see in the first line i toke the exception from the args and i wrote it to the console then i specified what will happen with the flow. You can set it to Continue then the debugger will continue as if no exception occurs, you can also throw the exception if you want.
- Using Exception Aspect in any function.
In the Program.cs we will create two function on that has the Exception Aspect aspect and the other doesn’t and they both throw Divide by zero exception and lets call them one by one and see the what happens.
static void Main(string args)
public static void FunWithAspect()
throw new DivideByZeroException();
public static void FunWithoutAspect()
throw new DivideByZeroException();
If you run the program now you will see that the exception is caught by the Exception Aspect and it is written to the Console.
Now try to run the other function that doesn’t have the ExceptionAspect you will see that it will throw the Divide by zero exception and the debugger will stop at this line.
You can also put the Exception aspect to the whole class and any exception inside that class will be caught by our ExceptionAspect.
Moreover you can apply the Exception Aspect or any other Aspects to a whole Namespace with a single line of code 🙂 It’s something called Multicast Check this Post if you want to know more about it 🙂