PostSharp

Multicast PostSharp Aspects C#

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 ūüôā

Advertisements

Exception handling using PostSharp C#

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.

  1. Creating a Console Application Project
    In Visual Studio menu select File>New>Project and select the ‚ÄúConsole Application‚ÄĚ template.
    1
  2. 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.
    2
    It will ask you for the Project you want to install PostSharp in, make sure your project is selected.
    3 Wait until it finish downloading and Installing PostSharp. It should not take long.
    4If you did everything right you will find a reference in your project to PostSharp
    5
  3. Creating our Exception Aspect Class
    Right Click the Project>Add>Class and name it whatever you want.
    6 Now we need to implement OnExceptionAspect which has a function OnException that we need to override to however you want to handle Exceptions.

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using PostSharp.Aspects;
    
    namespace AOP_Project
    {
           [Serializable]
           public class ExceptionAspect : OnExceptionAspect
           {
                   public override void OnException(MethodExecutionArgs args)
                    {
                           Console.WriteLine("Exception : " + args.Exception.Message + " at " + DateTime.Now);
                           args.FlowBehavior = FlowBehavior.Continue;
    
                          base.OnException(args);
                    }
           }
    }
    

    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.

  4. 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.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AOP_Project
    {
    class Program
    {
    static void Main(string[] args)
    {
    FunWithAspect();
    
    Console.ReadLine();
    }
    
    [ExceptionAspect]
    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.
    7
    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.
    8

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 ūüôā