我有这个代码。
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<ITestInterception, TestInterception>(new TransientLifetimeManager(),
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>());
container.Configure<Interception>()
.AddPolicy("MyPolicy")
.AddMatchingRule(new MemberNameMatchingRule("Test"))
.AddCallHandler<FaultHandler>();
try
{
var tester = container.Resolve<ITestInterception>();
tester.Test();
}
catch (Exception e)
{
Console.WriteLine(e.GetType() + "\n\n");
}
Console.ReadKey();
}
}
class AlwaysMatchingRule : IMatchingRule
{
public bool Matches(MethodBase member)
{
return true;
}
}
interface ITestInterception
{
void Test();
}
class TestInterception : ITestInterception
{
public void Test()
{
throw new ArgumentNullException("Null");
}
}
class FaultHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.WriteLine("Invoke called");
IMethodReturn result = getNext()(input, getNext);
Exception e = result.Exception;
if (e == null)
return result;
return input.CreateExceptionMethodReturn(new InvalidOperationException("In interceptor", e));
}
public int Order { get; set; }
}
当它运行时,我有 ResolutionFailedException。
依赖项解析失败,type = "TestUnity.ITestInterception",name = "(none)"。异常发生时:解决时。例外情况是:TypeLoadException - 来自程序集“Unity_ILEmit_InterfaceProxies,版本=0.0.0.0”的“DynamicModule.ns.Wrapped_ITestInterception_0e954c5db37c4c4ebf99acaee12e93f7”类型,
你能解释一下如何解决这个问题吗?