Nov 8, 2011 at 2:26 AM
Edited Nov 10, 2011 at 2:57 AM
The issue is that the Divide method is returning an int but you are handling the exception and not setting a return value so the result.ReturnValue is null.
You can't return a null return value for a value type (such as an int). (A NullReferenceException is being thrown by what looks to be generated Unity code.)
If you want to prove it to yourself change your interface to return a reference type (such as string):
public interface ICalc
int Add(int oprandA, int oprandB);
string Divide(int dividend, int divisor);
public class Calc : ICalc
public int Add(int oprandA, int oprandB)
return oprandA + oprandB;
public string Divide(int dividend, int divisor)
return (dividend / divisor).ToString();
If you run this code then the exception will be swallowed and null returned.
You could create a new method return but what will the default value be? If it's null then you are in the same situation.
One way to avoid returning null is to return the default value for the type in question:
result.Exception = null;
Type type = ((MethodInfo)input.MethodBase).ReturnType;
result.ReturnValue = Activator.CreateInstance(type);
If you add that code to the TraceBehavior then you will avoid the exception being thrown.
Also, it's usually not a good idea to swallow exceptions. Especially exceptions thrown from the target method. I can see swallowing exceptions that occur in the course
of performing the interception. For example if the interception is performing logging and the logging fails then you might want to swallow it so the application can continue even though
the logging failed -- this is what Enterprise Library does.
Enterprise Library support engineer