Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is CastleWindsor's BeginScope out of scope?

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?

like image 801
B. Clay Shannon-B. Crow Raven Avatar asked Dec 25 '13 00:12

B. Clay Shannon-B. Crow Raven


1 Answers

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

like image 109
Aaronaught Avatar answered Sep 21 '22 01:09

Aaronaught