c# - 温莎城堡拦截机

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

我正在尝试使用此页面中的代码,http://docs.castleproject.org/Windsor.Introduction-to-AOP-With-Castle.ashx并以流畅的方式注册拦截器。 但我抛出了这个错误。我尝试过温莎城堡 2.5 到 3.3 版本。所以拦截器的设置方式一定是非常基本的

类(class)

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));
}

错误

Type 'Castle.Proxies.ISomethingProxy' from assembly'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is attempting to implement an inaccessible interface.

最佳答案

答案

所以我找到了为什么会发生这种情况。显然,如果您创建内部类和接口(interface),您可以注册并解析它们,但向它们附加拦截器将不起作用

示例 - 错误将被触发的位置

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();
        }
    }
}

结论

所以总而言之,我的目的并不是首先创建内部类,而是制作一个演示来展示温莎城堡。但也许这可以帮助那些遇到与我相同错误的人..

关于c# - 温莎城堡拦截机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29651381/

相关文章:

c# - 如何编译运行单个class文件的cs文件?

c# - 反序列化json数据c#

c# - 使用简单注入(inject)器注册多个电子邮件输出服务

c# - C#代码与SQL Server 2008存储过程的性能比较

configuration - 使用 ".properties"文件的依赖注入(inject)

android - 如何将 Activity 注入(inject)另一个类(class)

c# - 使用 ScheduleJobs 调用计划

c# - WCF 中的 Excel 自动化

.net - 使用 WPF 实现 WinForms?

.net - 向客户端发送 Hibernate 代理类会出现哪些问题