2013-07-31 56 views
1

我有这段代码。注册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,

你能解释我如何解决这个问题呢?

+0

您正在使用哪个版本的Unity? – RoelF

+0

Unity 2版本 – lomomike

回答

1

InnerException消息说,这一切:

类型 'DynamicModule.ns.Wrapped_ITestInterception_c8a0c8bf8a3d48a5b7f85924fab5711f' 自组装 'Unity_ILEmit_InterfaceProxies,版本= 0.0.0.0,文化=中立,公钥=空' 试图实施无法访问的界面

你需要让你的界面市民:

public interface ITestInterception 
{ 
    void Test(); 
} 
0

到@ Roelf的答案是允许调用组件访问内部接口/对象的选择:

在具有该项目的在属性下的Unity配置,请添加:

[assembly: InternalsVisibleTo("Unity_ILEmit_InterfaceProxies")]