Intercepting Unity and Exclusion

I am currently dealing with a problem where I have many iterations and their implementations, all created with unity. these classes contain some methods that throw exceptions on a regular basis, and I wanted to create a dynamic proxy server around these classes so that I can catch all the exceptions that occur in the methods that handle them somewhere else.

As I play with Unity, I wonder if it is possible to do something similar using Unity Interception.

i.e. create a TransparentProxyInterceptor and wrap a try-catch block around the invocatino of these methods. Is this possible at all or am I going in the wrong direction? thanks

+4
source share
1 answer

Yes, Unity Interception (AOP) is a great way to handle exception handling. You can add all kinds of behavior, for example:

  • write to a log file or event log
  • send email
  • increase performance counter
  • automatic restart by timeout or blocking exception
  • repeat another mistake.

Your call handler will look something like this:

public override IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { IMethodReturn result = getNext()(input, getNext); if (result.Exception != null) { // do something } return result; } 
+6
source

Source: https://habr.com/ru/post/1286412/


All Articles