Windsor Interceptor Exception - PullRequest
       12

Windsor Interceptor Exception

0 голосов
/ 28 октября 2010

У меня есть контейнер 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 голосов
/ 04 ноября 2010

Кажется, это проблема в способе, которым 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));
           }         

Кажется, чтобы решить проблему.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...