使用此代码
[TestMethod]
public void Should_Wrap_Exception_ThrownByTarget()
{
var container = new UnityContainer();
container.RegisterType<Target>(
new Interceptor<VirtualMethodInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>());
container.AddNewExtension<Interception>();
var config = container.Configure<Interception>();
config.AddPolicy("1").AddCallHandler<ExceptionHandler>().AddMatchingRule<AlwaysMatches>();
var target = container.Resolve<Target>();
target.AlwaysThrows("foo");
}
public class AlwaysMatches : IMatchingRule
{
public bool Matches(MethodBase member)
{
return true;
}
}
public class ExceptionHandler : ICallHandler
{
public int Order { get; set; }
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
IMethodReturn r = getNext()(input, getNext);
if (r.Exception != null)
{
throw new InvalidOperationException("CallHandler", r.Exception);
}
return r;
}
}
public class Target
{
public virtual string AlwaysThrows(string foo)
{
throw new Exception("Boom!");
}
}
我得到一个看起来像这样的堆栈跟踪
at UnityExceptionAspect.Target.AlwaysThrows(String foo) in C:\VisualStudio\Evaluation\UnityExceptionAspect\Target.cs:line 9
at DynamicModule.ns.Wrapped_Target_c49f840ef38c41d7b4d5956223e95f73.<AlwaysThrows_DelegateImplementation>__0(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)
抱歉格式不好...
它肯定包含异常的原始来源,尽管它被有关生成类型的神秘信息所混淆。