【问题标题】:Castle Windsor Interceptor温莎城堡拦截器
【发布时间】:2015-06-21 11:34:11
【问题描述】:

我正在尝试使用此页面中的代码http://docs.castleproject.org/Windsor.Introduction-to-AOP-With-Castle.ashx 并以流畅的方式注册拦截器。 但是我抛出了这个错误。我已经尝试过从 2.5 到 3.3 的 Castle Windsor 版本。所以它必须是如何设置拦截器的非常基本的东西

public interface ISomething
{
    Int32 Augment(Int32 input);
    void DoSomething(String input);
    Int32 Property { get; set; }
}

class Something : ISomething
{
    public int Augment(int input) {
        return input + 1;
    }

    public void DoSomething(string input) {
        Console.WriteLine("I'm doing something: " + input);
    }

    public int Property { get; set; }
 }

public class DumpInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation) {
        Console.WriteLine("DumpInterceptorCalled on method " +
            invocation.Method.Name);
        invocation.Proceed();

        if (invocation.Method.ReturnType == typeof(Int32)) {
            invocation.ReturnValue = (Int32)invocation.ReturnValue + 1;
        }

        Console.WriteLine("DumpInterceptor returnvalue is " +
            (invocation.ReturnValue ?? "NULL"));
    }     
}

设置

Console.WriteLine("Run 2 - configuration fluent");
using (WindsorContainer container = new WindsorContainer())
{
    container.Register(
        Component.For<IInterceptor>()
        .ImplementedBy<DumpInterceptor>()
        .Named("myinterceptor"));
    container.Register(
        Component.For<ISomething>()
        .ImplementedBy<Something>()
     .Interceptors(InterceptorReference.ForKey("myinterceptor")).Anywhere);


    ISomething something = container.Resolve<ISomething>(); //Offending row

    something.DoSomething("");

    Console.WriteLine("Augment 10 returns " + something.Augment(10));
}

错误

键入“Castle.Proxies.ISomethingProxy”来自 程序集'DynamicProxyGenAssembly2,版本=0.0.0.0,文化=中性, PublicKeyToken=null' 试图实现一个不可访问的 界面。

【问题讨论】:

  • 如果我用 [Interceptor("myinterceptor")] 添加拦截器也是一样的
  • 我只是将您的代码复制粘贴到一个新的控制台应用程序中,它对我有用。你确定这是你的代码吗?
  • 在我将每个类和接口放在自己的文件中后,它实际上自行解决了。我还删除并阅读了城堡温莎。我想也许他们之前在主课上是内部课程,我不确定。它现在可以工作了……浪费了很多时间:)。感谢您尝试@YuvalItzchakov
  • 是的,就是这样。您可以注册和解析内部类,但不能向它们添加拦截器。嗯,偷偷摸摸:)
  • @Chris,不要犹豫,清理您的代码以仅保留相关信息,然后发布您的最新评论作为答案,这确实很重要,我会赞成跨度>

标签: c# .net dependency-injection castle-windsor interception


【解决方案1】:

答案

所以我找到了发生这种情况的原因。显然,如果您创建内部类和接口,您可以注册和解析它们,但将拦截器附加到它们将不起作用

示例 - 将触发错误的位置

class Program
{
    public static void Main(String [] args)
    {
        var container = new WindsorContainer();
        container.Register(Component.For<TestInterceptor>().Named("test"));
        container.Register(Component.For<InnerInterface>().ImplementedBy<InnerClass>().Interceptors(InterceptorReference.ForKey("test")).Anywhere);
        // this row below will throw the exception
        var innerClassInstance = container.Resolve<InnerInterface>();
    }

    class InnerClass : InnerInterface  { }

    interface InnerInterface { }

    class TestInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            throw new NotImplementedException();
        }
    }
}

结论

所以总结一下,我的意图并不是一开始就创建内部类,而是制作一个演示来展示温莎城堡。但是,如果他们遇到与我相同的错误,也许这可以帮助某人..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    相关资源
    最近更新 更多