castle-windsorwindsor-3.0

Castle Scoped Lifestyle not working


Trying to find the real cause of this and not having much fun!

Type is not resolved for member 'Castle.MicroKernel.Lifestyle.Scoped.CallContextLifetimeScope+SerializationReference,Castle.Windsor, Version=3.1.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc'. 

This looks like a bug considering I have nothing register with the container using this lifestyle.


Solution

  • I'm not sure what you are trying to do, but if your goal is implementing an IDependencyResolver (which looks like it since you are using scopes):

    If you are implementing an IDependencyResolver, don't try to be clever and inherit from your IDependencyScope implementation. Create the Resolver from scratch. This is how I implemented my dependency resolver (which works):

    public class WindsorDependencyResolver : IDependencyResolver {
        private readonly IWindsorContainer _container;
    
        public WindsorDependencyResolver(IWindsorContainer container)
        {
            _container = container;
        }
    
        public IDependencyScope BeginScope()
        {
            return new WindsorDependencyScope(_container);
        }
    
        public object GetService(Type serviceType)
        {
            return _container.Kernel.HasComponent(serviceType)
                       ? _container.Resolve(serviceType)
                       : null;
        }
    
        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _container.ResolveAll(serviceType).Cast<object>();
        }
    
        public void Dispose()
        {
        }
    }
    
    public class WindsorDependencyScope : IDependencyScope {
        private readonly IWindsorContainer _container;
        private readonly IDisposable _scope;
        private bool _disposed;
    
        public WindsorDependencyScope(IWindsorContainer container)
        {
            _container = container;
            _scope = _container.BeginScope();
        }
    
        public object GetService(Type serviceType)
        {
            EnsureNotDisposed();
            return _container.Kernel.HasComponent(serviceType)
                       ? _container.Kernel.Resolve(serviceType)
                       : null;
        }
    
        public IEnumerable<object> GetServices(Type serviceType)
        {
            EnsureNotDisposed();
            return _container.ResolveAll(serviceType).Cast<object>();
        }
    
        public void Dispose()
        {
            if(_disposed) return;
    
            _scope.Dispose();
            _disposed = true;
            GC.SuppressFinalize(this);
        }
    
        private void EnsureNotDisposed()
        {
            if(_disposed) throw new ObjectDisposedException("WindsorDependencyScope");
        }
    }
    

    And this was my first attempt (which produced your error):

    public class WindsorDependencyResolver : WindsorDependencyScope, IDependencyResolver {
        private readonly IWindsorContainer _container;
    
        public WindsorDependencyResolver(IWindsorContainer container)
            : base(container)
        {
            _container = container;
        }
    
        public IDependencyScope BeginScope()
        {
            return new WindsorDependencyScope(_container);
        }
    }