我有这段代码。注册Unity拦截器时的ResolutionFailedException
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。
分辨率依赖性的失败,类型= “TestUnity.ITestInterception”,名字= “(无)”。同时发生异常 :解决。例外情况是:TypeLoadException - 类型 'DynamicModule.ns.Wrapped_ITestInterception_0e954c5db37c4c4ebf99acaee12e93f7' 从程序集“Unity_ILEmit_InterfaceProxies,版本= 0.0.0.0,
你能解释我如何解决这个问题呢?
您正在使用哪个版本的Unity? – RoelF
Unity 2版本 – lomomike