We would like to remove redundant try catch blocks in our application.
Obviously an unity interceptor can implement a common handler and save lots of duplicate code.
But i havent found a way to suppress the exception in the intercepted method.
Current:
void InterceptedMethod
{
try
{
}
catch()
{
}
}
Intended:
void InterceptedMethod
{
//no try catch blocks
}
for example (using StreamReader sr= new StreamReader(some invalid path)) will throw an exception in the intercepted method, that will not be caught if i remove the existing try catch block.
The code after (result.Exception ! = null) is executing successfully.
But it currently serves only "before enter" and "after exit" scenarios.
I still need to remove the try catch blocks in the intercepted method. I know postsharp or castle windsor allows us to set properties.
what is the way with unity IOC?
For simple cases it is relatively straight forward to create an IInterceptionBehavior
that will swallow exceptions. For example:
public class SuppressExceptionBehavior : IInterceptionBehavior
{
public IEnumerable<Type> GetRequiredInterfaces() => new Type[0];
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
IMethodReturn result = getNext()(input, getNext);
if (result.Exception != null)
{
result.Exception = null;
}
return result;
}
public bool WillExecute => true;
}
In the above example if the method call returns an exception it is set to null which causes the exception to be swallowed.
The container could be configured like this:
var container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<ITenantStore, TenantStore>(
new InterceptionBehavior<SuppressExceptionBehavior>(),
new Interceptor<InterfaceInterceptor>());
This works well for simple methods like the one you mentioned: void InterceptedMethod() { }
. However for a general case you will have to analyze the expected return values to ensure that the correct types can be returned (or an exception will be raised).
For example, what if the method has a return value? Now you will have to set the result.ReturnValue
object to be a value that is compatible with the expected return type (or use input.CreateMethodReturn() with a valid value) . For reference types this can be null so should just work without any change but for value types (e.g. int) a specific value will need to be set. Another example that would need to be handled (to avoid an exception being thrown) is setting a default value for out parameters.