我有这段代码。
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)”。发生了异常 while:同时解决。例外是:TypeLoadException - Type 'DynamicModule.ns.Wrapped_ITestInterception_0e954c5db37c4c4ebf99acaee12e93f7' 来自程序集'Unity_ILEmit_InterfaceProxies,Version = 0.0.0.0,
你能解释一下如何解决这个问题吗?
答案 0 :(得分:1)
InnerException
消息说明了一切:
从程序集'Unity_ILEmit_InterfaceProxies输入'DynamicModule.ns.Wrapped_ITestInterception_c8a0c8bf8a3d48a5b7f85924fab5711f',Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'正在尝试实现无法访问的界面。
您需要公开您的界面:
public interface ITestInterception
{
void Test();
}
答案 1 :(得分:0)
@ Roelf的答案的另一种方法是允许调用程序集访问内部接口/对象:
在具有Unity配置的项目中,在“属性”下,添加:
[assembly: InternalsVisibleTo("Unity_ILEmit_InterfaceProxies")]