温莎拦截器例外

时间:2010-10-28 16:20:49

标签: dependency-injection inversion-of-control castle-windsor interceptor

我有一个Windsor容器,我正在使用InterceptorSelector和一个带有的LazyComponentLoader。

我的InterceptorSelector将InterceptorReference返回到我的InterceptorAdapter类,看起来像这样

public class InterceptorAdapter<T> : Castle.DynamicProxy.IInterceptor, IOnBehalfAware where T : IMyType
{
    private readonly T interceptor;

    public InterceptorAdapter(T interceptor)
    {
        this.interceptor = interceptor;
    }
    ....
}

所以InterceptorSelector做了

        public InterceptorReference[] SelectInterceptors(ComponentModel model, InterceptorReference[] interceptors)
        {
            var results = new List<InterceptorReference>();
            ....
            foreach (var interceptorType in someInterceptorTypes)
            {
                Type interceptorAdapterType = typeof(InterceptorAdapter<>).MakeGenericType(interceptorType);


               if (kernel.GetHandler(interceptorAdapterType) == null)
               { 
                 // need to do this or Castle complains it can't create my  
                 // interceptor...I guess LazyComponentLoaders don't work for InterceptorReferences?...  
                 // I suspect the problem may be here...
                 kernel.Register(lazyComponentLoader.Load(null, interceptorAdapterType, null));
               }                    

                results.Add(InterceptorReference.ForType(interceptorAdapterType));
            }
            return results.ToArray();
        }

我的InterceptorSelector第一次返回InterceptorReference时,事情很好。下一次,当它将InterceptorReference返回给具有不同泛型类型参数的InterceptorAdapter时,我得到了

Castle.MicroKernel.ComponentRegistrationException occurred
  Message=There is a component already registered for the given key interceptor
  Source=Castle.Windsor
  StackTrace:
       at Castle.MicroKernel.SubSystems.Naming.DefaultNamingSubSystem.Register(String key, IHandler handler) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\SubSystems\Naming\DefaultNamingSubSystem.cs:line 75
  InnerException: 

我的LazyComponentLoader只是

        public IRegistration Load(string key, Type service, IDictionary arguments)
        {
            ComponentRegistration<object> component = Component.For(service).Named(key);                

            if (arguments != null)
            {
                // merge is an extension method to merge dictionaries.
                component.DynamicParameters((k, d) => d.Merge(arguments));
            }
            return component;
        }

我的容器设置如下

        var container = new WindsorContainer();

        container.AddFacility<TypedFactoryFacility>();
        container.AddFacility("factories", new FactorySupportFacility());

        container.Register(Component.For<ILazyComponentLoader>().ImplementedBy<MyLazyComponentLoader>().LifeStyle.Singleton);

        container.Kernel.ProxyFactory.AddInterceptorSelector(new InterceptorSelector(container.Kernel, container.Resolve<ILazyComponentLoader>()));

有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

这似乎是Castle使用LazyComponentLoader解析拦截器的方式的问题。

在选择器中更改我的代码:

           if (kernel.GetHandler(interceptorType) == null)
           { 
             // need to do this or Castle complains it can't create my  
             // interceptor...I guess LazyComponentLoaders don't work for InterceptorReferences?...  
             // I suspect the problem may be here...
             kernel.Register(lazyComponentLoader.Load(null, interceptorType, null));
           }                    

            Type interceptorAdapterType = typeof(InterceptorAdapter<>).MakeGenericType(interceptorType);


           if (kernel.GetHandler(interceptorAdapterType) == null)
           { 
             // need to do this or Castle complains it can't create my  
             // interceptor...I guess LazyComponentLoaders don't work for InterceptorReferences?...  
             // I suspect the problem may be here...
             kernel.Register(lazyComponentLoader.Load(null, interceptorAdapterType, null));
           }         

似乎可以解决问题。