Why is CastleWindsor's BeginScope out of scope?
Asked Answered
T

1

9

I'm trying to add Castle Windsor to my Web API project, and am following this post, but am getting a compile-time error on this line of code:

this._scope = container.BeginScope();

...as "'Castle.Windsor.IWindsorContainer' does not contain a definition for 'BeginScope' and no extension method 'BeginScope' accepting a first argument of type 'Castle.Windsor.IWindsorContainer' could be found (are you missing a using directive or an assembly reference?)"

Here is the entire code so that it can be seen in context:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Dependencies;
using Castle.Windsor;
using Castle.MicroKernel.Registration;
using System.Web.Http;

namespace PlatypiPieServer
{
    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)
        {
            if (_container.Kernel.HasComponent(serviceType))
                return this._container.Resolve(serviceType);
            else
                return null;
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _container.ResolveAll(serviceType).Cast<object>();
        }

        public void Dispose()
        {
            _container.Dispose();
        }
    }

    public class WindsorDependencyScope : IDependencyScope
    {
        private readonly IWindsorContainer _container;
        private readonly IDisposable _scope;

        public WindsorDependencyScope(IWindsorContainer container)
        {
            this._container = container;
            this._scope = container.BeginScope();
        }

        public object GetService(Type serviceType)
        {
            if (_container.Kernel.HasComponent(serviceType))
                return _container.Resolve(serviceType);
            else
                return null;
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return this._container.ResolveAll(serviceType).Cast<object>();
        }

        public void Dispose()
        {
            this._scope.Dispose();
        }
    }

    public class ApiControllersInstaller : IWindsorInstaller
    {
        public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
        {
            container.Register(Classes.FromThisAssembly()
             .BasedOn<ApiController>()
             .LifestylePerWebRequest());
        }
    }
}

Where is BeginScope? Has it been deprecated?

Tremulant answered 25/12, 2013 at 0:20 Comment(0)
H
18

It's an extension method. You need to import the Castle.MicroKernel.Lifestyle namespace.

Hedvig answered 29/12, 2013 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.