1

我有这个代码。

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”类型,

你能解释一下如何解决这个问题吗?

4

2 回答 2

1

消息说明InnerException了一切:

来自程序集“Unity_ILEmit_InterfaceProxies,版本=0.0.0.0,文化=中性,PublicKeyToken=null”的类型“DynamicModule.ns.Wrapped_ITestInterception_c8a0c8bf8a3d48a5b7f85924fab5711f”正在尝试实现无法访问的接口

您需要公开您的界面:

public interface ITestInterception
{
    void Test();
}
于 2013-07-31T07:55:43.533 回答
0

@Roelf 答案的替代方法是允许调用程序集访问内部接口/对象:

在具有 Unity 配置的项目中,在“属性”下添加:

[assembly: InternalsVisibleTo("Unity_ILEmit_InterfaceProxies")]
于 2016-10-19T16:03:44.133 回答